dpt_i2o.c 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617
  1. /***************************************************************************
  2. dpti.c - description
  3. -------------------
  4. begin : Thu Sep 7 2000
  5. copyright : (C) 2000 by Adaptec
  6. July 30, 2001 First version being submitted
  7. for inclusion in the kernel. V2.4
  8. See Documentation/scsi/dpti.txt for history, notes, license info
  9. and credits
  10. ***************************************************************************/
  11. /***************************************************************************
  12. * *
  13. * This program is free software; you can redistribute it and/or modify *
  14. * it under the terms of the GNU General Public License as published by *
  15. * the Free Software Foundation; either version 2 of the License, or *
  16. * (at your option) any later version. *
  17. * *
  18. ***************************************************************************/
  19. /***************************************************************************
  20. * Sat Dec 20 2003 Go Taniguchi <go@turbolinux.co.jp>
  21. - Support 2.6 kernel and DMA-mapping
  22. - ioctl fix for raid tools
  23. - use schedule_timeout in long long loop
  24. **************************************************************************/
  25. /*#define DEBUG 1 */
  26. /*#define UARTDELAY 1 */
  27. #include <linux/module.h>
  28. MODULE_AUTHOR("Deanna Bonds, with _lots_ of help from Mark Salyzyn");
  29. MODULE_DESCRIPTION("Adaptec I2O RAID Driver");
  30. ////////////////////////////////////////////////////////////////
  31. #include <linux/ioctl.h> /* For SCSI-Passthrough */
  32. #include <linux/uaccess.h>
  33. #include <linux/stat.h>
  34. #include <linux/slab.h> /* for kmalloc() */
  35. #include <linux/pci.h> /* for PCI support */
  36. #include <linux/proc_fs.h>
  37. #include <linux/blkdev.h>
  38. #include <linux/delay.h> /* for udelay */
  39. #include <linux/interrupt.h>
  40. #include <linux/kernel.h> /* for printk */
  41. #include <linux/sched.h>
  42. #include <linux/reboot.h>
  43. #include <linux/spinlock.h>
  44. #include <linux/dma-mapping.h>
  45. #include <linux/timer.h>
  46. #include <linux/string.h>
  47. #include <linux/ioport.h>
  48. #include <linux/mutex.h>
  49. #include <asm/processor.h> /* for boot_cpu_data */
  50. #include <asm/pgtable.h>
  51. #include <asm/io.h> /* for virt_to_bus, etc. */
  52. #include <scsi/scsi.h>
  53. #include <scsi/scsi_cmnd.h>
  54. #include <scsi/scsi_device.h>
  55. #include <scsi/scsi_host.h>
  56. #include <scsi/scsi_tcq.h>
  57. #include "dpt/dptsig.h"
  58. #include "dpti.h"
  59. /*============================================================================
  60. * Create a binary signature - this is read by dptsig
  61. * Needed for our management apps
  62. *============================================================================
  63. */
  64. static DEFINE_MUTEX(adpt_mutex);
  65. static dpt_sig_S DPTI_sig = {
  66. {'d', 'P', 't', 'S', 'i', 'G'}, SIG_VERSION,
  67. #ifdef __i386__
  68. PROC_INTEL, PROC_386 | PROC_486 | PROC_PENTIUM | PROC_SEXIUM,
  69. #elif defined(__ia64__)
  70. PROC_INTEL, PROC_IA64,
  71. #elif defined(__sparc__)
  72. PROC_ULTRASPARC, PROC_ULTRASPARC,
  73. #elif defined(__alpha__)
  74. PROC_ALPHA, PROC_ALPHA,
  75. #else
  76. (-1),(-1),
  77. #endif
  78. FT_HBADRVR, 0, OEM_DPT, OS_LINUX, CAP_OVERLAP, DEV_ALL,
  79. ADF_ALL_SC5, 0, 0, DPT_VERSION, DPT_REVISION, DPT_SUBREVISION,
  80. DPT_MONTH, DPT_DAY, DPT_YEAR, "Adaptec Linux I2O RAID Driver"
  81. };
  82. /*============================================================================
  83. * Globals
  84. *============================================================================
  85. */
  86. static DEFINE_MUTEX(adpt_configuration_lock);
  87. static struct i2o_sys_tbl *sys_tbl;
  88. static dma_addr_t sys_tbl_pa;
  89. static int sys_tbl_ind;
  90. static int sys_tbl_len;
  91. static adpt_hba* hba_chain = NULL;
  92. static int hba_count = 0;
  93. static struct class *adpt_sysfs_class;
  94. static long adpt_unlocked_ioctl(struct file *, unsigned int, unsigned long);
  95. #ifdef CONFIG_COMPAT
  96. static long compat_adpt_ioctl(struct file *, unsigned int, unsigned long);
  97. #endif
  98. static const struct file_operations adpt_fops = {
  99. .unlocked_ioctl = adpt_unlocked_ioctl,
  100. .open = adpt_open,
  101. .release = adpt_close,
  102. #ifdef CONFIG_COMPAT
  103. .compat_ioctl = compat_adpt_ioctl,
  104. #endif
  105. .llseek = noop_llseek,
  106. };
  107. /* Structures and definitions for synchronous message posting.
  108. * See adpt_i2o_post_wait() for description
  109. * */
  110. struct adpt_i2o_post_wait_data
  111. {
  112. int status;
  113. u32 id;
  114. adpt_wait_queue_head_t *wq;
  115. struct adpt_i2o_post_wait_data *next;
  116. };
  117. static struct adpt_i2o_post_wait_data *adpt_post_wait_queue = NULL;
  118. static u32 adpt_post_wait_id = 0;
  119. static DEFINE_SPINLOCK(adpt_post_wait_lock);
  120. /*============================================================================
  121. * Functions
  122. *============================================================================
  123. */
  124. static inline int dpt_dma64(adpt_hba *pHba)
  125. {
  126. return (sizeof(dma_addr_t) > 4 && (pHba)->dma64);
  127. }
  128. static inline u32 dma_high(dma_addr_t addr)
  129. {
  130. return upper_32_bits(addr);
  131. }
  132. static inline u32 dma_low(dma_addr_t addr)
  133. {
  134. return (u32)addr;
  135. }
  136. static u8 adpt_read_blink_led(adpt_hba* host)
  137. {
  138. if (host->FwDebugBLEDflag_P) {
  139. if( readb(host->FwDebugBLEDflag_P) == 0xbc ){
  140. return readb(host->FwDebugBLEDvalue_P);
  141. }
  142. }
  143. return 0;
  144. }
  145. /*============================================================================
  146. * Scsi host template interface functions
  147. *============================================================================
  148. */
  149. #ifdef MODULE
  150. static struct pci_device_id dptids[] = {
  151. { PCI_DPT_VENDOR_ID, PCI_DPT_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
  152. { PCI_DPT_VENDOR_ID, PCI_DPT_RAPTOR_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
  153. { 0, }
  154. };
  155. #endif
  156. MODULE_DEVICE_TABLE(pci,dptids);
  157. static int adpt_detect(struct scsi_host_template* sht)
  158. {
  159. struct pci_dev *pDev = NULL;
  160. adpt_hba *pHba;
  161. adpt_hba *next;
  162. PINFO("Detecting Adaptec I2O RAID controllers...\n");
  163. /* search for all Adatpec I2O RAID cards */
  164. while ((pDev = pci_get_device( PCI_DPT_VENDOR_ID, PCI_ANY_ID, pDev))) {
  165. if(pDev->device == PCI_DPT_DEVICE_ID ||
  166. pDev->device == PCI_DPT_RAPTOR_DEVICE_ID){
  167. if(adpt_install_hba(sht, pDev) ){
  168. PERROR("Could not Init an I2O RAID device\n");
  169. PERROR("Will not try to detect others.\n");
  170. return hba_count-1;
  171. }
  172. pci_dev_get(pDev);
  173. }
  174. }
  175. /* In INIT state, Activate IOPs */
  176. for (pHba = hba_chain; pHba; pHba = next) {
  177. next = pHba->next;
  178. // Activate does get status , init outbound, and get hrt
  179. if (adpt_i2o_activate_hba(pHba) < 0) {
  180. adpt_i2o_delete_hba(pHba);
  181. }
  182. }
  183. /* Active IOPs in HOLD state */
  184. rebuild_sys_tab:
  185. if (hba_chain == NULL)
  186. return 0;
  187. /*
  188. * If build_sys_table fails, we kill everything and bail
  189. * as we can't init the IOPs w/o a system table
  190. */
  191. if (adpt_i2o_build_sys_table() < 0) {
  192. adpt_i2o_sys_shutdown();
  193. return 0;
  194. }
  195. PDEBUG("HBA's in HOLD state\n");
  196. /* If IOP don't get online, we need to rebuild the System table */
  197. for (pHba = hba_chain; pHba; pHba = pHba->next) {
  198. if (adpt_i2o_online_hba(pHba) < 0) {
  199. adpt_i2o_delete_hba(pHba);
  200. goto rebuild_sys_tab;
  201. }
  202. }
  203. /* Active IOPs now in OPERATIONAL state */
  204. PDEBUG("HBA's in OPERATIONAL state\n");
  205. printk("dpti: If you have a lot of devices this could take a few minutes.\n");
  206. for (pHba = hba_chain; pHba; pHba = next) {
  207. next = pHba->next;
  208. printk(KERN_INFO"%s: Reading the hardware resource table.\n", pHba->name);
  209. if (adpt_i2o_lct_get(pHba) < 0){
  210. adpt_i2o_delete_hba(pHba);
  211. continue;
  212. }
  213. if (adpt_i2o_parse_lct(pHba) < 0){
  214. adpt_i2o_delete_hba(pHba);
  215. continue;
  216. }
  217. adpt_inquiry(pHba);
  218. }
  219. adpt_sysfs_class = class_create(THIS_MODULE, "dpt_i2o");
  220. if (IS_ERR(adpt_sysfs_class)) {
  221. printk(KERN_WARNING"dpti: unable to create dpt_i2o class\n");
  222. adpt_sysfs_class = NULL;
  223. }
  224. for (pHba = hba_chain; pHba; pHba = next) {
  225. next = pHba->next;
  226. if (adpt_scsi_host_alloc(pHba, sht) < 0){
  227. adpt_i2o_delete_hba(pHba);
  228. continue;
  229. }
  230. pHba->initialized = TRUE;
  231. pHba->state &= ~DPTI_STATE_RESET;
  232. if (adpt_sysfs_class) {
  233. struct device *dev = device_create(adpt_sysfs_class,
  234. NULL, MKDEV(DPTI_I2O_MAJOR, pHba->unit), NULL,
  235. "dpti%d", pHba->unit);
  236. if (IS_ERR(dev)) {
  237. printk(KERN_WARNING"dpti%d: unable to "
  238. "create device in dpt_i2o class\n",
  239. pHba->unit);
  240. }
  241. }
  242. }
  243. // Register our control device node
  244. // nodes will need to be created in /dev to access this
  245. // the nodes can not be created from within the driver
  246. if (hba_count && register_chrdev(DPTI_I2O_MAJOR, DPT_DRIVER, &adpt_fops)) {
  247. adpt_i2o_sys_shutdown();
  248. return 0;
  249. }
  250. return hba_count;
  251. }
  252. static void adpt_release(adpt_hba *pHba)
  253. {
  254. struct Scsi_Host *shost = pHba->host;
  255. scsi_remove_host(shost);
  256. // adpt_i2o_quiesce_hba(pHba);
  257. adpt_i2o_delete_hba(pHba);
  258. scsi_host_put(shost);
  259. }
  260. static void adpt_inquiry(adpt_hba* pHba)
  261. {
  262. u32 msg[17];
  263. u32 *mptr;
  264. u32 *lenptr;
  265. int direction;
  266. int scsidir;
  267. u32 len;
  268. u32 reqlen;
  269. u8* buf;
  270. dma_addr_t addr;
  271. u8 scb[16];
  272. s32 rcode;
  273. memset(msg, 0, sizeof(msg));
  274. buf = dma_alloc_coherent(&pHba->pDev->dev, 80, &addr, GFP_KERNEL);
  275. if(!buf){
  276. printk(KERN_ERR"%s: Could not allocate buffer\n",pHba->name);
  277. return;
  278. }
  279. memset((void*)buf, 0, 36);
  280. len = 36;
  281. direction = 0x00000000;
  282. scsidir =0x40000000; // DATA IN (iop<--dev)
  283. if (dpt_dma64(pHba))
  284. reqlen = 17; // SINGLE SGE, 64 bit
  285. else
  286. reqlen = 14; // SINGLE SGE, 32 bit
  287. /* Stick the headers on */
  288. msg[0] = reqlen<<16 | SGL_OFFSET_12;
  289. msg[1] = (0xff<<24|HOST_TID<<12|ADAPTER_TID);
  290. msg[2] = 0;
  291. msg[3] = 0;
  292. // Adaptec/DPT Private stuff
  293. msg[4] = I2O_CMD_SCSI_EXEC|DPT_ORGANIZATION_ID<<16;
  294. msg[5] = ADAPTER_TID | 1<<16 /* Interpret*/;
  295. /* Direction, disconnect ok | sense data | simple queue , CDBLen */
  296. // I2O_SCB_FLAG_ENABLE_DISCONNECT |
  297. // I2O_SCB_FLAG_SIMPLE_QUEUE_TAG |
  298. // I2O_SCB_FLAG_SENSE_DATA_IN_MESSAGE;
  299. msg[6] = scsidir|0x20a00000| 6 /* cmd len*/;
  300. mptr=msg+7;
  301. memset(scb, 0, sizeof(scb));
  302. // Write SCSI command into the message - always 16 byte block
  303. scb[0] = INQUIRY;
  304. scb[1] = 0;
  305. scb[2] = 0;
  306. scb[3] = 0;
  307. scb[4] = 36;
  308. scb[5] = 0;
  309. // Don't care about the rest of scb
  310. memcpy(mptr, scb, sizeof(scb));
  311. mptr+=4;
  312. lenptr=mptr++; /* Remember me - fill in when we know */
  313. /* Now fill in the SGList and command */
  314. *lenptr = len;
  315. if (dpt_dma64(pHba)) {
  316. *mptr++ = (0x7C<<24)+(2<<16)+0x02; /* Enable 64 bit */
  317. *mptr++ = 1 << PAGE_SHIFT;
  318. *mptr++ = 0xD0000000|direction|len;
  319. *mptr++ = dma_low(addr);
  320. *mptr++ = dma_high(addr);
  321. } else {
  322. *mptr++ = 0xD0000000|direction|len;
  323. *mptr++ = addr;
  324. }
  325. // Send it on it's way
  326. rcode = adpt_i2o_post_wait(pHba, msg, reqlen<<2, 120);
  327. if (rcode != 0) {
  328. sprintf(pHba->detail, "Adaptec I2O RAID");
  329. printk(KERN_INFO "%s: Inquiry Error (%d)\n",pHba->name,rcode);
  330. if (rcode != -ETIME && rcode != -EINTR)
  331. dma_free_coherent(&pHba->pDev->dev, 80, buf, addr);
  332. } else {
  333. memset(pHba->detail, 0, sizeof(pHba->detail));
  334. memcpy(&(pHba->detail), "Vendor: Adaptec ", 16);
  335. memcpy(&(pHba->detail[16]), " Model: ", 8);
  336. memcpy(&(pHba->detail[24]), (u8*) &buf[16], 16);
  337. memcpy(&(pHba->detail[40]), " FW: ", 4);
  338. memcpy(&(pHba->detail[44]), (u8*) &buf[32], 4);
  339. pHba->detail[48] = '\0'; /* precautionary */
  340. dma_free_coherent(&pHba->pDev->dev, 80, buf, addr);
  341. }
  342. adpt_i2o_status_get(pHba);
  343. return ;
  344. }
  345. static int adpt_slave_configure(struct scsi_device * device)
  346. {
  347. struct Scsi_Host *host = device->host;
  348. adpt_hba* pHba;
  349. pHba = (adpt_hba *) host->hostdata[0];
  350. if (host->can_queue && device->tagged_supported) {
  351. scsi_change_queue_depth(device,
  352. host->can_queue - 1);
  353. }
  354. return 0;
  355. }
  356. static int adpt_queue_lck(struct scsi_cmnd * cmd, void (*done) (struct scsi_cmnd *))
  357. {
  358. adpt_hba* pHba = NULL;
  359. struct adpt_device* pDev = NULL; /* dpt per device information */
  360. cmd->scsi_done = done;
  361. /*
  362. * SCSI REQUEST_SENSE commands will be executed automatically by the
  363. * Host Adapter for any errors, so they should not be executed
  364. * explicitly unless the Sense Data is zero indicating that no error
  365. * occurred.
  366. */
  367. if ((cmd->cmnd[0] == REQUEST_SENSE) && (cmd->sense_buffer[0] != 0)) {
  368. cmd->result = (DID_OK << 16);
  369. cmd->scsi_done(cmd);
  370. return 0;
  371. }
  372. pHba = (adpt_hba*)cmd->device->host->hostdata[0];
  373. if (!pHba) {
  374. return FAILED;
  375. }
  376. rmb();
  377. if ((pHba->state) & DPTI_STATE_RESET)
  378. return SCSI_MLQUEUE_HOST_BUSY;
  379. // TODO if the cmd->device if offline then I may need to issue a bus rescan
  380. // followed by a get_lct to see if the device is there anymore
  381. if((pDev = (struct adpt_device*) (cmd->device->hostdata)) == NULL) {
  382. /*
  383. * First command request for this device. Set up a pointer
  384. * to the device structure. This should be a TEST_UNIT_READY
  385. * command from scan_scsis_single.
  386. */
  387. if ((pDev = adpt_find_device(pHba, (u32)cmd->device->channel, (u32)cmd->device->id, cmd->device->lun)) == NULL) {
  388. // TODO: if any luns are at this bus, scsi id then fake a TEST_UNIT_READY and INQUIRY response
  389. // with type 7F (for all luns less than the max for this bus,id) so the lun scan will continue.
  390. cmd->result = (DID_NO_CONNECT << 16);
  391. cmd->scsi_done(cmd);
  392. return 0;
  393. }
  394. cmd->device->hostdata = pDev;
  395. }
  396. pDev->pScsi_dev = cmd->device;
  397. /*
  398. * If we are being called from when the device is being reset,
  399. * delay processing of the command until later.
  400. */
  401. if (pDev->state & DPTI_DEV_RESET ) {
  402. return FAILED;
  403. }
  404. return adpt_scsi_to_i2o(pHba, cmd, pDev);
  405. }
  406. static DEF_SCSI_QCMD(adpt_queue)
  407. static int adpt_bios_param(struct scsi_device *sdev, struct block_device *dev,
  408. sector_t capacity, int geom[])
  409. {
  410. int heads=-1;
  411. int sectors=-1;
  412. int cylinders=-1;
  413. // *** First lets set the default geometry ****
  414. // If the capacity is less than ox2000
  415. if (capacity < 0x2000 ) { // floppy
  416. heads = 18;
  417. sectors = 2;
  418. }
  419. // else if between 0x2000 and 0x20000
  420. else if (capacity < 0x20000) {
  421. heads = 64;
  422. sectors = 32;
  423. }
  424. // else if between 0x20000 and 0x40000
  425. else if (capacity < 0x40000) {
  426. heads = 65;
  427. sectors = 63;
  428. }
  429. // else if between 0x4000 and 0x80000
  430. else if (capacity < 0x80000) {
  431. heads = 128;
  432. sectors = 63;
  433. }
  434. // else if greater than 0x80000
  435. else {
  436. heads = 255;
  437. sectors = 63;
  438. }
  439. cylinders = sector_div(capacity, heads * sectors);
  440. // Special case if CDROM
  441. if(sdev->type == 5) { // CDROM
  442. heads = 252;
  443. sectors = 63;
  444. cylinders = 1111;
  445. }
  446. geom[0] = heads;
  447. geom[1] = sectors;
  448. geom[2] = cylinders;
  449. PDEBUG("adpt_bios_param: exit\n");
  450. return 0;
  451. }
  452. static const char *adpt_info(struct Scsi_Host *host)
  453. {
  454. adpt_hba* pHba;
  455. pHba = (adpt_hba *) host->hostdata[0];
  456. return (char *) (pHba->detail);
  457. }
  458. static int adpt_show_info(struct seq_file *m, struct Scsi_Host *host)
  459. {
  460. struct adpt_device* d;
  461. int id;
  462. int chan;
  463. adpt_hba* pHba;
  464. int unit;
  465. // Find HBA (host bus adapter) we are looking for
  466. mutex_lock(&adpt_configuration_lock);
  467. for (pHba = hba_chain; pHba; pHba = pHba->next) {
  468. if (pHba->host == host) {
  469. break; /* found adapter */
  470. }
  471. }
  472. mutex_unlock(&adpt_configuration_lock);
  473. if (pHba == NULL) {
  474. return 0;
  475. }
  476. host = pHba->host;
  477. seq_printf(m, "Adaptec I2O RAID Driver Version: %s\n\n", DPT_I2O_VERSION);
  478. seq_printf(m, "%s\n", pHba->detail);
  479. seq_printf(m, "SCSI Host=scsi%d Control Node=/dev/%s irq=%d\n",
  480. pHba->host->host_no, pHba->name, host->irq);
  481. seq_printf(m, "\tpost fifo size = %d\n\treply fifo size = %d\n\tsg table size = %d\n\n",
  482. host->can_queue, (int) pHba->reply_fifo_size , host->sg_tablesize);
  483. seq_puts(m, "Devices:\n");
  484. for(chan = 0; chan < MAX_CHANNEL; chan++) {
  485. for(id = 0; id < MAX_ID; id++) {
  486. d = pHba->channel[chan].device[id];
  487. while(d) {
  488. seq_printf(m,"\t%-24.24s", d->pScsi_dev->vendor);
  489. seq_printf(m," Rev: %-8.8s\n", d->pScsi_dev->rev);
  490. unit = d->pI2o_dev->lct_data.tid;
  491. seq_printf(m, "\tTID=%d, (Channel=%d, Target=%d, Lun=%llu) (%s)\n\n",
  492. unit, (int)d->scsi_channel, (int)d->scsi_id, d->scsi_lun,
  493. scsi_device_online(d->pScsi_dev)? "online":"offline");
  494. d = d->next_lun;
  495. }
  496. }
  497. }
  498. return 0;
  499. }
  500. /*
  501. * Turn a struct scsi_cmnd * into a unique 32 bit 'context'.
  502. */
  503. static u32 adpt_cmd_to_context(struct scsi_cmnd *cmd)
  504. {
  505. return (u32)cmd->serial_number;
  506. }
  507. /*
  508. * Go from a u32 'context' to a struct scsi_cmnd * .
  509. * This could probably be made more efficient.
  510. */
  511. static struct scsi_cmnd *
  512. adpt_cmd_from_context(adpt_hba * pHba, u32 context)
  513. {
  514. struct scsi_cmnd * cmd;
  515. struct scsi_device * d;
  516. if (context == 0)
  517. return NULL;
  518. spin_unlock(pHba->host->host_lock);
  519. shost_for_each_device(d, pHba->host) {
  520. unsigned long flags;
  521. spin_lock_irqsave(&d->list_lock, flags);
  522. list_for_each_entry(cmd, &d->cmd_list, list) {
  523. if (((u32)cmd->serial_number == context)) {
  524. spin_unlock_irqrestore(&d->list_lock, flags);
  525. scsi_device_put(d);
  526. spin_lock(pHba->host->host_lock);
  527. return cmd;
  528. }
  529. }
  530. spin_unlock_irqrestore(&d->list_lock, flags);
  531. }
  532. spin_lock(pHba->host->host_lock);
  533. return NULL;
  534. }
  535. /*
  536. * Turn a pointer to ioctl reply data into an u32 'context'
  537. */
  538. static u32 adpt_ioctl_to_context(adpt_hba * pHba, void *reply)
  539. {
  540. #if BITS_PER_LONG == 32
  541. return (u32)(unsigned long)reply;
  542. #else
  543. ulong flags = 0;
  544. u32 nr, i;
  545. spin_lock_irqsave(pHba->host->host_lock, flags);
  546. nr = ARRAY_SIZE(pHba->ioctl_reply_context);
  547. for (i = 0; i < nr; i++) {
  548. if (pHba->ioctl_reply_context[i] == NULL) {
  549. pHba->ioctl_reply_context[i] = reply;
  550. break;
  551. }
  552. }
  553. spin_unlock_irqrestore(pHba->host->host_lock, flags);
  554. if (i >= nr) {
  555. printk(KERN_WARNING"%s: Too many outstanding "
  556. "ioctl commands\n", pHba->name);
  557. return (u32)-1;
  558. }
  559. return i;
  560. #endif
  561. }
  562. /*
  563. * Go from an u32 'context' to a pointer to ioctl reply data.
  564. */
  565. static void *adpt_ioctl_from_context(adpt_hba *pHba, u32 context)
  566. {
  567. #if BITS_PER_LONG == 32
  568. return (void *)(unsigned long)context;
  569. #else
  570. void *p = pHba->ioctl_reply_context[context];
  571. pHba->ioctl_reply_context[context] = NULL;
  572. return p;
  573. #endif
  574. }
  575. /*===========================================================================
  576. * Error Handling routines
  577. *===========================================================================
  578. */
  579. static int adpt_abort(struct scsi_cmnd * cmd)
  580. {
  581. adpt_hba* pHba = NULL; /* host bus adapter structure */
  582. struct adpt_device* dptdevice; /* dpt per device information */
  583. u32 msg[5];
  584. int rcode;
  585. if(cmd->serial_number == 0){
  586. return FAILED;
  587. }
  588. pHba = (adpt_hba*) cmd->device->host->hostdata[0];
  589. printk(KERN_INFO"%s: Trying to Abort\n",pHba->name);
  590. if ((dptdevice = (void*) (cmd->device->hostdata)) == NULL) {
  591. printk(KERN_ERR "%s: Unable to abort: No device in cmnd\n",pHba->name);
  592. return FAILED;
  593. }
  594. memset(msg, 0, sizeof(msg));
  595. msg[0] = FIVE_WORD_MSG_SIZE|SGL_OFFSET_0;
  596. msg[1] = I2O_CMD_SCSI_ABORT<<24|HOST_TID<<12|dptdevice->tid;
  597. msg[2] = 0;
  598. msg[3]= 0;
  599. msg[4] = adpt_cmd_to_context(cmd);
  600. if (pHba->host)
  601. spin_lock_irq(pHba->host->host_lock);
  602. rcode = adpt_i2o_post_wait(pHba, msg, sizeof(msg), FOREVER);
  603. if (pHba->host)
  604. spin_unlock_irq(pHba->host->host_lock);
  605. if (rcode != 0) {
  606. if(rcode == -EOPNOTSUPP ){
  607. printk(KERN_INFO"%s: Abort cmd not supported\n",pHba->name);
  608. return FAILED;
  609. }
  610. printk(KERN_INFO"%s: Abort failed.\n",pHba->name);
  611. return FAILED;
  612. }
  613. printk(KERN_INFO"%s: Abort complete.\n",pHba->name);
  614. return SUCCESS;
  615. }
  616. #define I2O_DEVICE_RESET 0x27
  617. // This is the same for BLK and SCSI devices
  618. // NOTE this is wrong in the i2o.h definitions
  619. // This is not currently supported by our adapter but we issue it anyway
  620. static int adpt_device_reset(struct scsi_cmnd* cmd)
  621. {
  622. adpt_hba* pHba;
  623. u32 msg[4];
  624. u32 rcode;
  625. int old_state;
  626. struct adpt_device* d = cmd->device->hostdata;
  627. pHba = (void*) cmd->device->host->hostdata[0];
  628. printk(KERN_INFO"%s: Trying to reset device\n",pHba->name);
  629. if (!d) {
  630. printk(KERN_INFO"%s: Reset Device: Device Not found\n",pHba->name);
  631. return FAILED;
  632. }
  633. memset(msg, 0, sizeof(msg));
  634. msg[0] = FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
  635. msg[1] = (I2O_DEVICE_RESET<<24|HOST_TID<<12|d->tid);
  636. msg[2] = 0;
  637. msg[3] = 0;
  638. if (pHba->host)
  639. spin_lock_irq(pHba->host->host_lock);
  640. old_state = d->state;
  641. d->state |= DPTI_DEV_RESET;
  642. rcode = adpt_i2o_post_wait(pHba, msg,sizeof(msg), FOREVER);
  643. d->state = old_state;
  644. if (pHba->host)
  645. spin_unlock_irq(pHba->host->host_lock);
  646. if (rcode != 0) {
  647. if(rcode == -EOPNOTSUPP ){
  648. printk(KERN_INFO"%s: Device reset not supported\n",pHba->name);
  649. return FAILED;
  650. }
  651. printk(KERN_INFO"%s: Device reset failed\n",pHba->name);
  652. return FAILED;
  653. } else {
  654. printk(KERN_INFO"%s: Device reset successful\n",pHba->name);
  655. return SUCCESS;
  656. }
  657. }
  658. #define I2O_HBA_BUS_RESET 0x87
  659. // This version of bus reset is called by the eh_error handler
  660. static int adpt_bus_reset(struct scsi_cmnd* cmd)
  661. {
  662. adpt_hba* pHba;
  663. u32 msg[4];
  664. u32 rcode;
  665. pHba = (adpt_hba*)cmd->device->host->hostdata[0];
  666. memset(msg, 0, sizeof(msg));
  667. printk(KERN_WARNING"%s: Bus reset: SCSI Bus %d: tid: %d\n",pHba->name, cmd->device->channel,pHba->channel[cmd->device->channel].tid );
  668. msg[0] = FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
  669. msg[1] = (I2O_HBA_BUS_RESET<<24|HOST_TID<<12|pHba->channel[cmd->device->channel].tid);
  670. msg[2] = 0;
  671. msg[3] = 0;
  672. if (pHba->host)
  673. spin_lock_irq(pHba->host->host_lock);
  674. rcode = adpt_i2o_post_wait(pHba, msg,sizeof(msg), FOREVER);
  675. if (pHba->host)
  676. spin_unlock_irq(pHba->host->host_lock);
  677. if (rcode != 0) {
  678. printk(KERN_WARNING"%s: Bus reset failed.\n",pHba->name);
  679. return FAILED;
  680. } else {
  681. printk(KERN_WARNING"%s: Bus reset success.\n",pHba->name);
  682. return SUCCESS;
  683. }
  684. }
  685. // This version of reset is called by the eh_error_handler
  686. static int __adpt_reset(struct scsi_cmnd* cmd)
  687. {
  688. adpt_hba* pHba;
  689. int rcode;
  690. char name[32];
  691. pHba = (adpt_hba*)cmd->device->host->hostdata[0];
  692. strncpy(name, pHba->name, sizeof(name));
  693. printk(KERN_WARNING"%s: Hba Reset: scsi id %d: tid: %d\n", name, cmd->device->channel, pHba->channel[cmd->device->channel].tid);
  694. rcode = adpt_hba_reset(pHba);
  695. if(rcode == 0){
  696. printk(KERN_WARNING"%s: HBA reset complete\n", name);
  697. return SUCCESS;
  698. } else {
  699. printk(KERN_WARNING"%s: HBA reset failed (%x)\n", name, rcode);
  700. return FAILED;
  701. }
  702. }
  703. static int adpt_reset(struct scsi_cmnd* cmd)
  704. {
  705. int rc;
  706. spin_lock_irq(cmd->device->host->host_lock);
  707. rc = __adpt_reset(cmd);
  708. spin_unlock_irq(cmd->device->host->host_lock);
  709. return rc;
  710. }
  711. // This version of reset is called by the ioctls and indirectly from eh_error_handler via adpt_reset
  712. static int adpt_hba_reset(adpt_hba* pHba)
  713. {
  714. int rcode;
  715. pHba->state |= DPTI_STATE_RESET;
  716. // Activate does get status , init outbound, and get hrt
  717. if ((rcode=adpt_i2o_activate_hba(pHba)) < 0) {
  718. printk(KERN_ERR "%s: Could not activate\n", pHba->name);
  719. adpt_i2o_delete_hba(pHba);
  720. return rcode;
  721. }
  722. if ((rcode=adpt_i2o_build_sys_table()) < 0) {
  723. adpt_i2o_delete_hba(pHba);
  724. return rcode;
  725. }
  726. PDEBUG("%s: in HOLD state\n",pHba->name);
  727. if ((rcode=adpt_i2o_online_hba(pHba)) < 0) {
  728. adpt_i2o_delete_hba(pHba);
  729. return rcode;
  730. }
  731. PDEBUG("%s: in OPERATIONAL state\n",pHba->name);
  732. if ((rcode=adpt_i2o_lct_get(pHba)) < 0){
  733. adpt_i2o_delete_hba(pHba);
  734. return rcode;
  735. }
  736. if ((rcode=adpt_i2o_reparse_lct(pHba)) < 0){
  737. adpt_i2o_delete_hba(pHba);
  738. return rcode;
  739. }
  740. pHba->state &= ~DPTI_STATE_RESET;
  741. adpt_fail_posted_scbs(pHba);
  742. return 0; /* return success */
  743. }
  744. /*===========================================================================
  745. *
  746. *===========================================================================
  747. */
  748. static void adpt_i2o_sys_shutdown(void)
  749. {
  750. adpt_hba *pHba, *pNext;
  751. struct adpt_i2o_post_wait_data *p1, *old;
  752. printk(KERN_INFO"Shutting down Adaptec I2O controllers.\n");
  753. printk(KERN_INFO" This could take a few minutes if there are many devices attached\n");
  754. /* Delete all IOPs from the controller chain */
  755. /* They should have already been released by the
  756. * scsi-core
  757. */
  758. for (pHba = hba_chain; pHba; pHba = pNext) {
  759. pNext = pHba->next;
  760. adpt_i2o_delete_hba(pHba);
  761. }
  762. /* Remove any timedout entries from the wait queue. */
  763. // spin_lock_irqsave(&adpt_post_wait_lock, flags);
  764. /* Nothing should be outstanding at this point so just
  765. * free them
  766. */
  767. for(p1 = adpt_post_wait_queue; p1;) {
  768. old = p1;
  769. p1 = p1->next;
  770. kfree(old);
  771. }
  772. // spin_unlock_irqrestore(&adpt_post_wait_lock, flags);
  773. adpt_post_wait_queue = NULL;
  774. printk(KERN_INFO "Adaptec I2O controllers down.\n");
  775. }
  776. static int adpt_install_hba(struct scsi_host_template* sht, struct pci_dev* pDev)
  777. {
  778. adpt_hba* pHba = NULL;
  779. adpt_hba* p = NULL;
  780. ulong base_addr0_phys = 0;
  781. ulong base_addr1_phys = 0;
  782. u32 hba_map0_area_size = 0;
  783. u32 hba_map1_area_size = 0;
  784. void __iomem *base_addr_virt = NULL;
  785. void __iomem *msg_addr_virt = NULL;
  786. int dma64 = 0;
  787. int raptorFlag = FALSE;
  788. if(pci_enable_device(pDev)) {
  789. return -EINVAL;
  790. }
  791. if (pci_request_regions(pDev, "dpt_i2o")) {
  792. PERROR("dpti: adpt_config_hba: pci request region failed\n");
  793. return -EINVAL;
  794. }
  795. pci_set_master(pDev);
  796. /*
  797. * See if we should enable dma64 mode.
  798. */
  799. if (sizeof(dma_addr_t) > 4 &&
  800. pci_set_dma_mask(pDev, DMA_BIT_MASK(64)) == 0) {
  801. if (dma_get_required_mask(&pDev->dev) > DMA_BIT_MASK(32))
  802. dma64 = 1;
  803. }
  804. if (!dma64 && pci_set_dma_mask(pDev, DMA_BIT_MASK(32)) != 0)
  805. return -EINVAL;
  806. /* adapter only supports message blocks below 4GB */
  807. pci_set_consistent_dma_mask(pDev, DMA_BIT_MASK(32));
  808. base_addr0_phys = pci_resource_start(pDev,0);
  809. hba_map0_area_size = pci_resource_len(pDev,0);
  810. // Check if standard PCI card or single BAR Raptor
  811. if(pDev->device == PCI_DPT_DEVICE_ID){
  812. if(pDev->subsystem_device >=0xc032 && pDev->subsystem_device <= 0xc03b){
  813. // Raptor card with this device id needs 4M
  814. hba_map0_area_size = 0x400000;
  815. } else { // Not Raptor - it is a PCI card
  816. if(hba_map0_area_size > 0x100000 ){
  817. hba_map0_area_size = 0x100000;
  818. }
  819. }
  820. } else {// Raptor split BAR config
  821. // Use BAR1 in this configuration
  822. base_addr1_phys = pci_resource_start(pDev,1);
  823. hba_map1_area_size = pci_resource_len(pDev,1);
  824. raptorFlag = TRUE;
  825. }
  826. #if BITS_PER_LONG == 64
  827. /*
  828. * The original Adaptec 64 bit driver has this comment here:
  829. * "x86_64 machines need more optimal mappings"
  830. *
  831. * I assume some HBAs report ridiculously large mappings
  832. * and we need to limit them on platforms with IOMMUs.
  833. */
  834. if (raptorFlag == TRUE) {
  835. if (hba_map0_area_size > 128)
  836. hba_map0_area_size = 128;
  837. if (hba_map1_area_size > 524288)
  838. hba_map1_area_size = 524288;
  839. } else {
  840. if (hba_map0_area_size > 524288)
  841. hba_map0_area_size = 524288;
  842. }
  843. #endif
  844. base_addr_virt = ioremap(base_addr0_phys,hba_map0_area_size);
  845. if (!base_addr_virt) {
  846. pci_release_regions(pDev);
  847. PERROR("dpti: adpt_config_hba: io remap failed\n");
  848. return -EINVAL;
  849. }
  850. if(raptorFlag == TRUE) {
  851. msg_addr_virt = ioremap(base_addr1_phys, hba_map1_area_size );
  852. if (!msg_addr_virt) {
  853. PERROR("dpti: adpt_config_hba: io remap failed on BAR1\n");
  854. iounmap(base_addr_virt);
  855. pci_release_regions(pDev);
  856. return -EINVAL;
  857. }
  858. } else {
  859. msg_addr_virt = base_addr_virt;
  860. }
  861. // Allocate and zero the data structure
  862. pHba = kzalloc(sizeof(adpt_hba), GFP_KERNEL);
  863. if (!pHba) {
  864. if (msg_addr_virt != base_addr_virt)
  865. iounmap(msg_addr_virt);
  866. iounmap(base_addr_virt);
  867. pci_release_regions(pDev);
  868. return -ENOMEM;
  869. }
  870. mutex_lock(&adpt_configuration_lock);
  871. if(hba_chain != NULL){
  872. for(p = hba_chain; p->next; p = p->next);
  873. p->next = pHba;
  874. } else {
  875. hba_chain = pHba;
  876. }
  877. pHba->next = NULL;
  878. pHba->unit = hba_count;
  879. sprintf(pHba->name, "dpti%d", hba_count);
  880. hba_count++;
  881. mutex_unlock(&adpt_configuration_lock);
  882. pHba->pDev = pDev;
  883. pHba->base_addr_phys = base_addr0_phys;
  884. // Set up the Virtual Base Address of the I2O Device
  885. pHba->base_addr_virt = base_addr_virt;
  886. pHba->msg_addr_virt = msg_addr_virt;
  887. pHba->irq_mask = base_addr_virt+0x30;
  888. pHba->post_port = base_addr_virt+0x40;
  889. pHba->reply_port = base_addr_virt+0x44;
  890. pHba->hrt = NULL;
  891. pHba->lct = NULL;
  892. pHba->lct_size = 0;
  893. pHba->status_block = NULL;
  894. pHba->post_count = 0;
  895. pHba->state = DPTI_STATE_RESET;
  896. pHba->pDev = pDev;
  897. pHba->devices = NULL;
  898. pHba->dma64 = dma64;
  899. // Initializing the spinlocks
  900. spin_lock_init(&pHba->state_lock);
  901. spin_lock_init(&adpt_post_wait_lock);
  902. if(raptorFlag == 0){
  903. printk(KERN_INFO "Adaptec I2O RAID controller"
  904. " %d at %p size=%x irq=%d%s\n",
  905. hba_count-1, base_addr_virt,
  906. hba_map0_area_size, pDev->irq,
  907. dma64 ? " (64-bit DMA)" : "");
  908. } else {
  909. printk(KERN_INFO"Adaptec I2O RAID controller %d irq=%d%s\n",
  910. hba_count-1, pDev->irq,
  911. dma64 ? " (64-bit DMA)" : "");
  912. printk(KERN_INFO" BAR0 %p - size= %x\n",base_addr_virt,hba_map0_area_size);
  913. printk(KERN_INFO" BAR1 %p - size= %x\n",msg_addr_virt,hba_map1_area_size);
  914. }
  915. if (request_irq (pDev->irq, adpt_isr, IRQF_SHARED, pHba->name, pHba)) {
  916. printk(KERN_ERR"%s: Couldn't register IRQ %d\n", pHba->name, pDev->irq);
  917. adpt_i2o_delete_hba(pHba);
  918. return -EINVAL;
  919. }
  920. return 0;
  921. }
  922. static void adpt_i2o_delete_hba(adpt_hba* pHba)
  923. {
  924. adpt_hba* p1;
  925. adpt_hba* p2;
  926. struct i2o_device* d;
  927. struct i2o_device* next;
  928. int i;
  929. int j;
  930. struct adpt_device* pDev;
  931. struct adpt_device* pNext;
  932. mutex_lock(&adpt_configuration_lock);
  933. if(pHba->host){
  934. free_irq(pHba->host->irq, pHba);
  935. }
  936. p2 = NULL;
  937. for( p1 = hba_chain; p1; p2 = p1,p1=p1->next){
  938. if(p1 == pHba) {
  939. if(p2) {
  940. p2->next = p1->next;
  941. } else {
  942. hba_chain = p1->next;
  943. }
  944. break;
  945. }
  946. }
  947. hba_count--;
  948. mutex_unlock(&adpt_configuration_lock);
  949. iounmap(pHba->base_addr_virt);
  950. pci_release_regions(pHba->pDev);
  951. if(pHba->msg_addr_virt != pHba->base_addr_virt){
  952. iounmap(pHba->msg_addr_virt);
  953. }
  954. if(pHba->FwDebugBuffer_P)
  955. iounmap(pHba->FwDebugBuffer_P);
  956. if(pHba->hrt) {
  957. dma_free_coherent(&pHba->pDev->dev,
  958. pHba->hrt->num_entries * pHba->hrt->entry_len << 2,
  959. pHba->hrt, pHba->hrt_pa);
  960. }
  961. if(pHba->lct) {
  962. dma_free_coherent(&pHba->pDev->dev, pHba->lct_size,
  963. pHba->lct, pHba->lct_pa);
  964. }
  965. if(pHba->status_block) {
  966. dma_free_coherent(&pHba->pDev->dev, sizeof(i2o_status_block),
  967. pHba->status_block, pHba->status_block_pa);
  968. }
  969. if(pHba->reply_pool) {
  970. dma_free_coherent(&pHba->pDev->dev,
  971. pHba->reply_fifo_size * REPLY_FRAME_SIZE * 4,
  972. pHba->reply_pool, pHba->reply_pool_pa);
  973. }
  974. for(d = pHba->devices; d ; d = next){
  975. next = d->next;
  976. kfree(d);
  977. }
  978. for(i = 0 ; i < pHba->top_scsi_channel ; i++){
  979. for(j = 0; j < MAX_ID; j++){
  980. if(pHba->channel[i].device[j] != NULL){
  981. for(pDev = pHba->channel[i].device[j]; pDev; pDev = pNext){
  982. pNext = pDev->next_lun;
  983. kfree(pDev);
  984. }
  985. }
  986. }
  987. }
  988. pci_dev_put(pHba->pDev);
  989. if (adpt_sysfs_class)
  990. device_destroy(adpt_sysfs_class,
  991. MKDEV(DPTI_I2O_MAJOR, pHba->unit));
  992. kfree(pHba);
  993. if(hba_count <= 0){
  994. unregister_chrdev(DPTI_I2O_MAJOR, DPT_DRIVER);
  995. if (adpt_sysfs_class) {
  996. class_destroy(adpt_sysfs_class);
  997. adpt_sysfs_class = NULL;
  998. }
  999. }
  1000. }
  1001. static struct adpt_device* adpt_find_device(adpt_hba* pHba, u32 chan, u32 id, u64 lun)
  1002. {
  1003. struct adpt_device* d;
  1004. if(chan < 0 || chan >= MAX_CHANNEL)
  1005. return NULL;
  1006. d = pHba->channel[chan].device[id];
  1007. if(!d || d->tid == 0) {
  1008. return NULL;
  1009. }
  1010. /* If it is the only lun at that address then this should match*/
  1011. if(d->scsi_lun == lun){
  1012. return d;
  1013. }
  1014. /* else we need to look through all the luns */
  1015. for(d=d->next_lun ; d ; d = d->next_lun){
  1016. if(d->scsi_lun == lun){
  1017. return d;
  1018. }
  1019. }
  1020. return NULL;
  1021. }
  1022. static int adpt_i2o_post_wait(adpt_hba* pHba, u32* msg, int len, int timeout)
  1023. {
  1024. // I used my own version of the WAIT_QUEUE_HEAD
  1025. // to handle some version differences
  1026. // When embedded in the kernel this could go back to the vanilla one
  1027. ADPT_DECLARE_WAIT_QUEUE_HEAD(adpt_wq_i2o_post);
  1028. int status = 0;
  1029. ulong flags = 0;
  1030. struct adpt_i2o_post_wait_data *p1, *p2;
  1031. struct adpt_i2o_post_wait_data *wait_data =
  1032. kmalloc(sizeof(struct adpt_i2o_post_wait_data), GFP_ATOMIC);
  1033. DECLARE_WAITQUEUE(wait, current);
  1034. if (!wait_data)
  1035. return -ENOMEM;
  1036. /*
  1037. * The spin locking is needed to keep anyone from playing
  1038. * with the queue pointers and id while we do the same
  1039. */
  1040. spin_lock_irqsave(&adpt_post_wait_lock, flags);
  1041. // TODO we need a MORE unique way of getting ids
  1042. // to support async LCT get
  1043. wait_data->next = adpt_post_wait_queue;
  1044. adpt_post_wait_queue = wait_data;
  1045. adpt_post_wait_id++;
  1046. adpt_post_wait_id &= 0x7fff;
  1047. wait_data->id = adpt_post_wait_id;
  1048. spin_unlock_irqrestore(&adpt_post_wait_lock, flags);
  1049. wait_data->wq = &adpt_wq_i2o_post;
  1050. wait_data->status = -ETIMEDOUT;
  1051. add_wait_queue(&adpt_wq_i2o_post, &wait);
  1052. msg[2] |= 0x80000000 | ((u32)wait_data->id);
  1053. timeout *= HZ;
  1054. if((status = adpt_i2o_post_this(pHba, msg, len)) == 0){
  1055. set_current_state(TASK_INTERRUPTIBLE);
  1056. if(pHba->host)
  1057. spin_unlock_irq(pHba->host->host_lock);
  1058. if (!timeout)
  1059. schedule();
  1060. else{
  1061. timeout = schedule_timeout(timeout);
  1062. if (timeout == 0) {
  1063. // I/O issued, but cannot get result in
  1064. // specified time. Freeing resorces is
  1065. // dangerous.
  1066. status = -ETIME;
  1067. }
  1068. }
  1069. if(pHba->host)
  1070. spin_lock_irq(pHba->host->host_lock);
  1071. }
  1072. remove_wait_queue(&adpt_wq_i2o_post, &wait);
  1073. if(status == -ETIMEDOUT){
  1074. printk(KERN_INFO"dpti%d: POST WAIT TIMEOUT\n",pHba->unit);
  1075. // We will have to free the wait_data memory during shutdown
  1076. return status;
  1077. }
  1078. /* Remove the entry from the queue. */
  1079. p2 = NULL;
  1080. spin_lock_irqsave(&adpt_post_wait_lock, flags);
  1081. for(p1 = adpt_post_wait_queue; p1; p2 = p1, p1 = p1->next) {
  1082. if(p1 == wait_data) {
  1083. if(p1->status == I2O_DETAIL_STATUS_UNSUPPORTED_FUNCTION ) {
  1084. status = -EOPNOTSUPP;
  1085. }
  1086. if(p2) {
  1087. p2->next = p1->next;
  1088. } else {
  1089. adpt_post_wait_queue = p1->next;
  1090. }
  1091. break;
  1092. }
  1093. }
  1094. spin_unlock_irqrestore(&adpt_post_wait_lock, flags);
  1095. kfree(wait_data);
  1096. return status;
  1097. }
  1098. static s32 adpt_i2o_post_this(adpt_hba* pHba, u32* data, int len)
  1099. {
  1100. u32 m = EMPTY_QUEUE;
  1101. u32 __iomem *msg;
  1102. ulong timeout = jiffies + 30*HZ;
  1103. do {
  1104. rmb();
  1105. m = readl(pHba->post_port);
  1106. if (m != EMPTY_QUEUE) {
  1107. break;
  1108. }
  1109. if(time_after(jiffies,timeout)){
  1110. printk(KERN_WARNING"dpti%d: Timeout waiting for message frame!\n", pHba->unit);
  1111. return -ETIMEDOUT;
  1112. }
  1113. schedule_timeout_uninterruptible(1);
  1114. } while(m == EMPTY_QUEUE);
  1115. msg = pHba->msg_addr_virt + m;
  1116. memcpy_toio(msg, data, len);
  1117. wmb();
  1118. //post message
  1119. writel(m, pHba->post_port);
  1120. wmb();
  1121. return 0;
  1122. }
  1123. static void adpt_i2o_post_wait_complete(u32 context, int status)
  1124. {
  1125. struct adpt_i2o_post_wait_data *p1 = NULL;
  1126. /*
  1127. * We need to search through the adpt_post_wait
  1128. * queue to see if the given message is still
  1129. * outstanding. If not, it means that the IOP
  1130. * took longer to respond to the message than we
  1131. * had allowed and timer has already expired.
  1132. * Not much we can do about that except log
  1133. * it for debug purposes, increase timeout, and recompile
  1134. *
  1135. * Lock needed to keep anyone from moving queue pointers
  1136. * around while we're looking through them.
  1137. */
  1138. context &= 0x7fff;
  1139. spin_lock(&adpt_post_wait_lock);
  1140. for(p1 = adpt_post_wait_queue; p1; p1 = p1->next) {
  1141. if(p1->id == context) {
  1142. p1->status = status;
  1143. spin_unlock(&adpt_post_wait_lock);
  1144. wake_up_interruptible(p1->wq);
  1145. return;
  1146. }
  1147. }
  1148. spin_unlock(&adpt_post_wait_lock);
  1149. // If this happens we lose commands that probably really completed
  1150. printk(KERN_DEBUG"dpti: Could Not find task %d in wait queue\n",context);
  1151. printk(KERN_DEBUG" Tasks in wait queue:\n");
  1152. for(p1 = adpt_post_wait_queue; p1; p1 = p1->next) {
  1153. printk(KERN_DEBUG" %d\n",p1->id);
  1154. }
  1155. return;
  1156. }
  1157. static s32 adpt_i2o_reset_hba(adpt_hba* pHba)
  1158. {
  1159. u32 msg[8];
  1160. u8* status;
  1161. dma_addr_t addr;
  1162. u32 m = EMPTY_QUEUE ;
  1163. ulong timeout = jiffies + (TMOUT_IOPRESET*HZ);
  1164. if(pHba->initialized == FALSE) { // First time reset should be quick
  1165. timeout = jiffies + (25*HZ);
  1166. } else {
  1167. adpt_i2o_quiesce_hba(pHba);
  1168. }
  1169. do {
  1170. rmb();
  1171. m = readl(pHba->post_port);
  1172. if (m != EMPTY_QUEUE) {
  1173. break;
  1174. }
  1175. if(time_after(jiffies,timeout)){
  1176. printk(KERN_WARNING"Timeout waiting for message!\n");
  1177. return -ETIMEDOUT;
  1178. }
  1179. schedule_timeout_uninterruptible(1);
  1180. } while (m == EMPTY_QUEUE);
  1181. status = dma_alloc_coherent(&pHba->pDev->dev, 4, &addr, GFP_KERNEL);
  1182. if(status == NULL) {
  1183. adpt_send_nop(pHba, m);
  1184. printk(KERN_ERR"IOP reset failed - no free memory.\n");
  1185. return -ENOMEM;
  1186. }
  1187. memset(status,0,4);
  1188. msg[0]=EIGHT_WORD_MSG_SIZE|SGL_OFFSET_0;
  1189. msg[1]=I2O_CMD_ADAPTER_RESET<<24|HOST_TID<<12|ADAPTER_TID;
  1190. msg[2]=0;
  1191. msg[3]=0;
  1192. msg[4]=0;
  1193. msg[5]=0;
  1194. msg[6]=dma_low(addr);
  1195. msg[7]=dma_high(addr);
  1196. memcpy_toio(pHba->msg_addr_virt+m, msg, sizeof(msg));
  1197. wmb();
  1198. writel(m, pHba->post_port);
  1199. wmb();
  1200. while(*status == 0){
  1201. if(time_after(jiffies,timeout)){
  1202. printk(KERN_WARNING"%s: IOP Reset Timeout\n",pHba->name);
  1203. /* We lose 4 bytes of "status" here, but we cannot
  1204. free these because controller may awake and corrupt
  1205. those bytes at any time */
  1206. /* dma_free_coherent(&pHba->pDev->dev, 4, buf, addr); */
  1207. return -ETIMEDOUT;
  1208. }
  1209. rmb();
  1210. schedule_timeout_uninterruptible(1);
  1211. }
  1212. if(*status == 0x01 /*I2O_EXEC_IOP_RESET_IN_PROGRESS*/) {
  1213. PDEBUG("%s: Reset in progress...\n", pHba->name);
  1214. // Here we wait for message frame to become available
  1215. // indicated that reset has finished
  1216. do {
  1217. rmb();
  1218. m = readl(pHba->post_port);
  1219. if (m != EMPTY_QUEUE) {
  1220. break;
  1221. }
  1222. if(time_after(jiffies,timeout)){
  1223. printk(KERN_ERR "%s:Timeout waiting for IOP Reset.\n",pHba->name);
  1224. /* We lose 4 bytes of "status" here, but we
  1225. cannot free these because controller may
  1226. awake and corrupt those bytes at any time */
  1227. /* dma_free_coherent(&pHba->pDev->dev, 4, buf, addr); */
  1228. return -ETIMEDOUT;
  1229. }
  1230. schedule_timeout_uninterruptible(1);
  1231. } while (m == EMPTY_QUEUE);
  1232. // Flush the offset
  1233. adpt_send_nop(pHba, m);
  1234. }
  1235. adpt_i2o_status_get(pHba);
  1236. if(*status == 0x02 ||
  1237. pHba->status_block->iop_state != ADAPTER_STATE_RESET) {
  1238. printk(KERN_WARNING"%s: Reset reject, trying to clear\n",
  1239. pHba->name);
  1240. } else {
  1241. PDEBUG("%s: Reset completed.\n", pHba->name);
  1242. }
  1243. dma_free_coherent(&pHba->pDev->dev, 4, status, addr);
  1244. #ifdef UARTDELAY
  1245. // This delay is to allow someone attached to the card through the debug UART to
  1246. // set up the dump levels that they want before the rest of the initialization sequence
  1247. adpt_delay(20000);
  1248. #endif
  1249. return 0;
  1250. }
  1251. static int adpt_i2o_parse_lct(adpt_hba* pHba)
  1252. {
  1253. int i;
  1254. int max;
  1255. int tid;
  1256. struct i2o_device *d;
  1257. i2o_lct *lct = pHba->lct;
  1258. u8 bus_no = 0;
  1259. s16 scsi_id;
  1260. u64 scsi_lun;
  1261. u32 buf[10]; // larger than 7, or 8 ...
  1262. struct adpt_device* pDev;
  1263. if (lct == NULL) {
  1264. printk(KERN_ERR "%s: LCT is empty???\n",pHba->name);
  1265. return -1;
  1266. }
  1267. max = lct->table_size;
  1268. max -= 3;
  1269. max /= 9;
  1270. for(i=0;i<max;i++) {
  1271. if( lct->lct_entry[i].user_tid != 0xfff){
  1272. /*
  1273. * If we have hidden devices, we need to inform the upper layers about
  1274. * the possible maximum id reference to handle device access when
  1275. * an array is disassembled. This code has no other purpose but to
  1276. * allow us future access to devices that are currently hidden
  1277. * behind arrays, hotspares or have not been configured (JBOD mode).
  1278. */
  1279. if( lct->lct_entry[i].class_id != I2O_CLASS_RANDOM_BLOCK_STORAGE &&
  1280. lct->lct_entry[i].class_id != I2O_CLASS_SCSI_PERIPHERAL &&
  1281. lct->lct_entry[i].class_id != I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL ){
  1282. continue;
  1283. }
  1284. tid = lct->lct_entry[i].tid;
  1285. // I2O_DPT_DEVICE_INFO_GROUP_NO;
  1286. if(adpt_i2o_query_scalar(pHba, tid, 0x8000, -1, buf, 32)<0) {
  1287. continue;
  1288. }
  1289. bus_no = buf[0]>>16;
  1290. scsi_id = buf[1];
  1291. scsi_lun = scsilun_to_int((struct scsi_lun *)&buf[2]);
  1292. if(bus_no >= MAX_CHANNEL) { // Something wrong skip it
  1293. printk(KERN_WARNING"%s: Channel number %d out of range \n", pHba->name, bus_no);
  1294. continue;
  1295. }
  1296. if (scsi_id >= MAX_ID){
  1297. printk(KERN_WARNING"%s: SCSI ID %d out of range \n", pHba->name, bus_no);
  1298. continue;
  1299. }
  1300. if(bus_no > pHba->top_scsi_channel){
  1301. pHba->top_scsi_channel = bus_no;
  1302. }
  1303. if(scsi_id > pHba->top_scsi_id){
  1304. pHba->top_scsi_id = scsi_id;
  1305. }
  1306. if(scsi_lun > pHba->top_scsi_lun){
  1307. pHba->top_scsi_lun = scsi_lun;
  1308. }
  1309. continue;
  1310. }
  1311. d = kmalloc(sizeof(struct i2o_device), GFP_KERNEL);
  1312. if(d==NULL)
  1313. {
  1314. printk(KERN_CRIT"%s: Out of memory for I2O device data.\n",pHba->name);
  1315. return -ENOMEM;
  1316. }
  1317. d->controller = pHba;
  1318. d->next = NULL;
  1319. memcpy(&d->lct_data, &lct->lct_entry[i], sizeof(i2o_lct_entry));
  1320. d->flags = 0;
  1321. tid = d->lct_data.tid;
  1322. adpt_i2o_report_hba_unit(pHba, d);
  1323. adpt_i2o_install_device(pHba, d);
  1324. }
  1325. bus_no = 0;
  1326. for(d = pHba->devices; d ; d = d->next) {
  1327. if(d->lct_data.class_id == I2O_CLASS_BUS_ADAPTER_PORT ||
  1328. d->lct_data.class_id == I2O_CLASS_FIBRE_CHANNEL_PORT){
  1329. tid = d->lct_data.tid;
  1330. // TODO get the bus_no from hrt-but for now they are in order
  1331. //bus_no =
  1332. if(bus_no > pHba->top_scsi_channel){
  1333. pHba->top_scsi_channel = bus_no;
  1334. }
  1335. pHba->channel[bus_no].type = d->lct_data.class_id;
  1336. pHba->channel[bus_no].tid = tid;
  1337. if(adpt_i2o_query_scalar(pHba, tid, 0x0200, -1, buf, 28)>=0)
  1338. {
  1339. pHba->channel[bus_no].scsi_id = buf[1];
  1340. PDEBUG("Bus %d - SCSI ID %d.\n", bus_no, buf[1]);
  1341. }
  1342. // TODO remove - this is just until we get from hrt
  1343. bus_no++;
  1344. if(bus_no >= MAX_CHANNEL) { // Something wrong skip it
  1345. printk(KERN_WARNING"%s: Channel number %d out of range - LCT\n", pHba->name, bus_no);
  1346. break;
  1347. }
  1348. }
  1349. }
  1350. // Setup adpt_device table
  1351. for(d = pHba->devices; d ; d = d->next) {
  1352. if(d->lct_data.class_id == I2O_CLASS_RANDOM_BLOCK_STORAGE ||
  1353. d->lct_data.class_id == I2O_CLASS_SCSI_PERIPHERAL ||
  1354. d->lct_data.class_id == I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL ){
  1355. tid = d->lct_data.tid;
  1356. scsi_id = -1;
  1357. // I2O_DPT_DEVICE_INFO_GROUP_NO;
  1358. if(adpt_i2o_query_scalar(pHba, tid, 0x8000, -1, buf, 32)>=0) {
  1359. bus_no = buf[0]>>16;
  1360. scsi_id = buf[1];
  1361. scsi_lun = scsilun_to_int((struct scsi_lun *)&buf[2]);
  1362. if(bus_no >= MAX_CHANNEL) { // Something wrong skip it
  1363. continue;
  1364. }
  1365. if (scsi_id >= MAX_ID) {
  1366. continue;
  1367. }
  1368. if( pHba->channel[bus_no].device[scsi_id] == NULL){
  1369. pDev = kzalloc(sizeof(struct adpt_device),GFP_KERNEL);
  1370. if(pDev == NULL) {
  1371. return -ENOMEM;
  1372. }
  1373. pHba->channel[bus_no].device[scsi_id] = pDev;
  1374. } else {
  1375. for( pDev = pHba->channel[bus_no].device[scsi_id];
  1376. pDev->next_lun; pDev = pDev->next_lun){
  1377. }
  1378. pDev->next_lun = kzalloc(sizeof(struct adpt_device),GFP_KERNEL);
  1379. if(pDev->next_lun == NULL) {
  1380. return -ENOMEM;
  1381. }
  1382. pDev = pDev->next_lun;
  1383. }
  1384. pDev->tid = tid;
  1385. pDev->scsi_channel = bus_no;
  1386. pDev->scsi_id = scsi_id;
  1387. pDev->scsi_lun = scsi_lun;
  1388. pDev->pI2o_dev = d;
  1389. d->owner = pDev;
  1390. pDev->type = (buf[0])&0xff;
  1391. pDev->flags = (buf[0]>>8)&0xff;
  1392. if(scsi_id > pHba->top_scsi_id){
  1393. pHba->top_scsi_id = scsi_id;
  1394. }
  1395. if(scsi_lun > pHba->top_scsi_lun){
  1396. pHba->top_scsi_lun = scsi_lun;
  1397. }
  1398. }
  1399. if(scsi_id == -1){
  1400. printk(KERN_WARNING"Could not find SCSI ID for %s\n",
  1401. d->lct_data.identity_tag);
  1402. }
  1403. }
  1404. }
  1405. return 0;
  1406. }
  1407. /*
  1408. * Each I2O controller has a chain of devices on it - these match
  1409. * the useful parts of the LCT of the board.
  1410. */
  1411. static int adpt_i2o_install_device(adpt_hba* pHba, struct i2o_device *d)
  1412. {
  1413. mutex_lock(&adpt_configuration_lock);
  1414. d->controller=pHba;
  1415. d->owner=NULL;
  1416. d->next=pHba->devices;
  1417. d->prev=NULL;
  1418. if (pHba->devices != NULL){
  1419. pHba->devices->prev=d;
  1420. }
  1421. pHba->devices=d;
  1422. *d->dev_name = 0;
  1423. mutex_unlock(&adpt_configuration_lock);
  1424. return 0;
  1425. }
  1426. static int adpt_open(struct inode *inode, struct file *file)
  1427. {
  1428. int minor;
  1429. adpt_hba* pHba;
  1430. mutex_lock(&adpt_mutex);
  1431. //TODO check for root access
  1432. //
  1433. minor = iminor(inode);
  1434. if (minor >= hba_count) {
  1435. mutex_unlock(&adpt_mutex);
  1436. return -ENXIO;
  1437. }
  1438. mutex_lock(&adpt_configuration_lock);
  1439. for (pHba = hba_chain; pHba; pHba = pHba->next) {
  1440. if (pHba->unit == minor) {
  1441. break; /* found adapter */
  1442. }
  1443. }
  1444. if (pHba == NULL) {
  1445. mutex_unlock(&adpt_configuration_lock);
  1446. mutex_unlock(&adpt_mutex);
  1447. return -ENXIO;
  1448. }
  1449. // if(pHba->in_use){
  1450. // mutex_unlock(&adpt_configuration_lock);
  1451. // return -EBUSY;
  1452. // }
  1453. pHba->in_use = 1;
  1454. mutex_unlock(&adpt_configuration_lock);
  1455. mutex_unlock(&adpt_mutex);
  1456. return 0;
  1457. }
  1458. static int adpt_close(struct inode *inode, struct file *file)
  1459. {
  1460. int minor;
  1461. adpt_hba* pHba;
  1462. minor = iminor(inode);
  1463. if (minor >= hba_count) {
  1464. return -ENXIO;
  1465. }
  1466. mutex_lock(&adpt_configuration_lock);
  1467. for (pHba = hba_chain; pHba; pHba = pHba->next) {
  1468. if (pHba->unit == minor) {
  1469. break; /* found adapter */
  1470. }
  1471. }
  1472. mutex_unlock(&adpt_configuration_lock);
  1473. if (pHba == NULL) {
  1474. return -ENXIO;
  1475. }
  1476. pHba->in_use = 0;
  1477. return 0;
  1478. }
  1479. static int adpt_i2o_passthru(adpt_hba* pHba, u32 __user *arg)
  1480. {
  1481. u32 msg[MAX_MESSAGE_SIZE];
  1482. u32* reply = NULL;
  1483. u32 size = 0;
  1484. u32 reply_size = 0;
  1485. u32 __user *user_msg = arg;
  1486. u32 __user * user_reply = NULL;
  1487. void **sg_list = NULL;
  1488. u32 sg_offset = 0;
  1489. u32 sg_count = 0;
  1490. int sg_index = 0;
  1491. u32 i = 0;
  1492. u32 rcode = 0;
  1493. void *p = NULL;
  1494. dma_addr_t addr;
  1495. ulong flags = 0;
  1496. memset(&msg, 0, MAX_MESSAGE_SIZE*4);
  1497. // get user msg size in u32s
  1498. if(get_user(size, &user_msg[0])){
  1499. return -EFAULT;
  1500. }
  1501. size = size>>16;
  1502. user_reply = &user_msg[size];
  1503. if(size > MAX_MESSAGE_SIZE){
  1504. return -EFAULT;
  1505. }
  1506. size *= 4; // Convert to bytes
  1507. /* Copy in the user's I2O command */
  1508. if(copy_from_user(msg, user_msg, size)) {
  1509. return -EFAULT;
  1510. }
  1511. get_user(reply_size, &user_reply[0]);
  1512. reply_size = reply_size>>16;
  1513. if(reply_size > REPLY_FRAME_SIZE){
  1514. reply_size = REPLY_FRAME_SIZE;
  1515. }
  1516. reply_size *= 4;
  1517. reply = kzalloc(REPLY_FRAME_SIZE*4, GFP_KERNEL);
  1518. if(reply == NULL) {
  1519. printk(KERN_WARNING"%s: Could not allocate reply buffer\n",pHba->name);
  1520. return -ENOMEM;
  1521. }
  1522. sg_offset = (msg[0]>>4)&0xf;
  1523. msg[2] = 0x40000000; // IOCTL context
  1524. msg[3] = adpt_ioctl_to_context(pHba, reply);
  1525. if (msg[3] == (u32)-1) {
  1526. rcode = -EBUSY;
  1527. goto free;
  1528. }
  1529. sg_list = kcalloc(pHba->sg_tablesize, sizeof(*sg_list), GFP_KERNEL);
  1530. if (!sg_list) {
  1531. rcode = -ENOMEM;
  1532. goto free;
  1533. }
  1534. if(sg_offset) {
  1535. // TODO add 64 bit API
  1536. struct sg_simple_element *sg = (struct sg_simple_element*) (msg+sg_offset);
  1537. sg_count = (size - sg_offset*4) / sizeof(struct sg_simple_element);
  1538. if (sg_count > pHba->sg_tablesize){
  1539. printk(KERN_DEBUG"%s:IOCTL SG List too large (%u)\n", pHba->name,sg_count);
  1540. rcode = -EINVAL;
  1541. goto free;
  1542. }
  1543. for(i = 0; i < sg_count; i++) {
  1544. int sg_size;
  1545. if (!(sg[i].flag_count & 0x10000000 /*I2O_SGL_FLAGS_SIMPLE_ADDRESS_ELEMENT*/)) {
  1546. printk(KERN_DEBUG"%s:Bad SG element %d - not simple (%x)\n",pHba->name,i, sg[i].flag_count);
  1547. rcode = -EINVAL;
  1548. goto cleanup;
  1549. }
  1550. sg_size = sg[i].flag_count & 0xffffff;
  1551. /* Allocate memory for the transfer */
  1552. p = dma_alloc_coherent(&pHba->pDev->dev, sg_size, &addr, GFP_KERNEL);
  1553. if(!p) {
  1554. printk(KERN_DEBUG"%s: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
  1555. pHba->name,sg_size,i,sg_count);
  1556. rcode = -ENOMEM;
  1557. goto cleanup;
  1558. }
  1559. sg_list[sg_index++] = p; // sglist indexed with input frame, not our internal frame.
  1560. /* Copy in the user's SG buffer if necessary */
  1561. if(sg[i].flag_count & 0x04000000 /*I2O_SGL_FLAGS_DIR*/) {
  1562. // sg_simple_element API is 32 bit
  1563. if (copy_from_user(p,(void __user *)(ulong)sg[i].addr_bus, sg_size)) {
  1564. printk(KERN_DEBUG"%s: Could not copy SG buf %d FROM user\n",pHba->name,i);
  1565. rcode = -EFAULT;
  1566. goto cleanup;
  1567. }
  1568. }
  1569. /* sg_simple_element API is 32 bit, but addr < 4GB */
  1570. sg[i].addr_bus = addr;
  1571. }
  1572. }
  1573. do {
  1574. /*
  1575. * Stop any new commands from enterring the
  1576. * controller while processing the ioctl
  1577. */
  1578. if (pHba->host) {
  1579. scsi_block_requests(pHba->host);
  1580. spin_lock_irqsave(pHba->host->host_lock, flags);
  1581. }
  1582. rcode = adpt_i2o_post_wait(pHba, msg, size, FOREVER);
  1583. if (rcode != 0)
  1584. printk("adpt_i2o_passthru: post wait failed %d %p\n",
  1585. rcode, reply);
  1586. if (pHba->host) {
  1587. spin_unlock_irqrestore(pHba->host->host_lock, flags);
  1588. scsi_unblock_requests(pHba->host);
  1589. }
  1590. } while (rcode == -ETIMEDOUT);
  1591. if(rcode){
  1592. goto cleanup;
  1593. }
  1594. if(sg_offset) {
  1595. /* Copy back the Scatter Gather buffers back to user space */
  1596. u32 j;
  1597. // TODO add 64 bit API
  1598. struct sg_simple_element* sg;
  1599. int sg_size;
  1600. // re-acquire the original message to handle correctly the sg copy operation
  1601. memset(&msg, 0, MAX_MESSAGE_SIZE*4);
  1602. // get user msg size in u32s
  1603. if(get_user(size, &user_msg[0])){
  1604. rcode = -EFAULT;
  1605. goto cleanup;
  1606. }
  1607. size = size>>16;
  1608. size *= 4;
  1609. if (size > MAX_MESSAGE_SIZE) {
  1610. rcode = -EINVAL;
  1611. goto cleanup;
  1612. }
  1613. /* Copy in the user's I2O command */
  1614. if (copy_from_user (msg, user_msg, size)) {
  1615. rcode = -EFAULT;
  1616. goto cleanup;
  1617. }
  1618. sg_count = (size - sg_offset*4) / sizeof(struct sg_simple_element);
  1619. // TODO add 64 bit API
  1620. sg = (struct sg_simple_element*)(msg + sg_offset);
  1621. for (j = 0; j < sg_count; j++) {
  1622. /* Copy out the SG list to user's buffer if necessary */
  1623. if(! (sg[j].flag_count & 0x4000000 /*I2O_SGL_FLAGS_DIR*/)) {
  1624. sg_size = sg[j].flag_count & 0xffffff;
  1625. // sg_simple_element API is 32 bit
  1626. if (copy_to_user((void __user *)(ulong)sg[j].addr_bus,sg_list[j], sg_size)) {
  1627. printk(KERN_WARNING"%s: Could not copy %p TO user %x\n",pHba->name, sg_list[j], sg[j].addr_bus);
  1628. rcode = -EFAULT;
  1629. goto cleanup;
  1630. }
  1631. }
  1632. }
  1633. }
  1634. /* Copy back the reply to user space */
  1635. if (reply_size) {
  1636. // we wrote our own values for context - now restore the user supplied ones
  1637. if(copy_from_user(reply+2, user_msg+2, sizeof(u32)*2)) {
  1638. printk(KERN_WARNING"%s: Could not copy message context FROM user\n",pHba->name);
  1639. rcode = -EFAULT;
  1640. }
  1641. if(copy_to_user(user_reply, reply, reply_size)) {
  1642. printk(KERN_WARNING"%s: Could not copy reply TO user\n",pHba->name);
  1643. rcode = -EFAULT;
  1644. }
  1645. }
  1646. cleanup:
  1647. if (rcode != -ETIME && rcode != -EINTR) {
  1648. struct sg_simple_element *sg =
  1649. (struct sg_simple_element*) (msg +sg_offset);
  1650. while(sg_index) {
  1651. if(sg_list[--sg_index]) {
  1652. dma_free_coherent(&pHba->pDev->dev,
  1653. sg[sg_index].flag_count & 0xffffff,
  1654. sg_list[sg_index],
  1655. sg[sg_index].addr_bus);
  1656. }
  1657. }
  1658. }
  1659. free:
  1660. kfree(sg_list);
  1661. kfree(reply);
  1662. return rcode;
  1663. }
  1664. #if defined __ia64__
  1665. static void adpt_ia64_info(sysInfo_S* si)
  1666. {
  1667. // This is all the info we need for now
  1668. // We will add more info as our new
  1669. // managmenent utility requires it
  1670. si->processorType = PROC_IA64;
  1671. }
  1672. #endif
  1673. #if defined __sparc__
  1674. static void adpt_sparc_info(sysInfo_S* si)
  1675. {
  1676. // This is all the info we need for now
  1677. // We will add more info as our new
  1678. // managmenent utility requires it
  1679. si->processorType = PROC_ULTRASPARC;
  1680. }
  1681. #endif
  1682. #if defined __alpha__
  1683. static void adpt_alpha_info(sysInfo_S* si)
  1684. {
  1685. // This is all the info we need for now
  1686. // We will add more info as our new
  1687. // managmenent utility requires it
  1688. si->processorType = PROC_ALPHA;
  1689. }
  1690. #endif
  1691. #if defined __i386__
  1692. #include <uapi/asm/vm86.h>
  1693. static void adpt_i386_info(sysInfo_S* si)
  1694. {
  1695. // This is all the info we need for now
  1696. // We will add more info as our new
  1697. // managmenent utility requires it
  1698. switch (boot_cpu_data.x86) {
  1699. case CPU_386:
  1700. si->processorType = PROC_386;
  1701. break;
  1702. case CPU_486:
  1703. si->processorType = PROC_486;
  1704. break;
  1705. case CPU_586:
  1706. si->processorType = PROC_PENTIUM;
  1707. break;
  1708. default: // Just in case
  1709. si->processorType = PROC_PENTIUM;
  1710. break;
  1711. }
  1712. }
  1713. #endif
  1714. /*
  1715. * This routine returns information about the system. This does not effect
  1716. * any logic and if the info is wrong - it doesn't matter.
  1717. */
  1718. /* Get all the info we can not get from kernel services */
  1719. static int adpt_system_info(void __user *buffer)
  1720. {
  1721. sysInfo_S si;
  1722. memset(&si, 0, sizeof(si));
  1723. si.osType = OS_LINUX;
  1724. si.osMajorVersion = 0;
  1725. si.osMinorVersion = 0;
  1726. si.osRevision = 0;
  1727. si.busType = SI_PCI_BUS;
  1728. si.processorFamily = DPTI_sig.dsProcessorFamily;
  1729. #if defined __i386__
  1730. adpt_i386_info(&si);
  1731. #elif defined (__ia64__)
  1732. adpt_ia64_info(&si);
  1733. #elif defined(__sparc__)
  1734. adpt_sparc_info(&si);
  1735. #elif defined (__alpha__)
  1736. adpt_alpha_info(&si);
  1737. #else
  1738. si.processorType = 0xff ;
  1739. #endif
  1740. if (copy_to_user(buffer, &si, sizeof(si))){
  1741. printk(KERN_WARNING"dpti: Could not copy buffer TO user\n");
  1742. return -EFAULT;
  1743. }
  1744. return 0;
  1745. }
  1746. static int adpt_ioctl(struct inode *inode, struct file *file, uint cmd, ulong arg)
  1747. {
  1748. int minor;
  1749. int error = 0;
  1750. adpt_hba* pHba;
  1751. ulong flags = 0;
  1752. void __user *argp = (void __user *)arg;
  1753. minor = iminor(inode);
  1754. if (minor >= DPTI_MAX_HBA){
  1755. return -ENXIO;
  1756. }
  1757. mutex_lock(&adpt_configuration_lock);
  1758. for (pHba = hba_chain; pHba; pHba = pHba->next) {
  1759. if (pHba->unit == minor) {
  1760. break; /* found adapter */
  1761. }
  1762. }
  1763. mutex_unlock(&adpt_configuration_lock);
  1764. if(pHba == NULL){
  1765. return -ENXIO;
  1766. }
  1767. while((volatile u32) pHba->state & DPTI_STATE_RESET )
  1768. schedule_timeout_uninterruptible(2);
  1769. switch (cmd) {
  1770. // TODO: handle 3 cases
  1771. case DPT_SIGNATURE:
  1772. if (copy_to_user(argp, &DPTI_sig, sizeof(DPTI_sig))) {
  1773. return -EFAULT;
  1774. }
  1775. break;
  1776. case I2OUSRCMD:
  1777. return adpt_i2o_passthru(pHba, argp);
  1778. case DPT_CTRLINFO:{
  1779. drvrHBAinfo_S HbaInfo;
  1780. #define FLG_OSD_PCI_VALID 0x0001
  1781. #define FLG_OSD_DMA 0x0002
  1782. #define FLG_OSD_I2O 0x0004
  1783. memset(&HbaInfo, 0, sizeof(HbaInfo));
  1784. HbaInfo.drvrHBAnum = pHba->unit;
  1785. HbaInfo.baseAddr = (ulong) pHba->base_addr_phys;
  1786. HbaInfo.blinkState = adpt_read_blink_led(pHba);
  1787. HbaInfo.pciBusNum = pHba->pDev->bus->number;
  1788. HbaInfo.pciDeviceNum=PCI_SLOT(pHba->pDev->devfn);
  1789. HbaInfo.Interrupt = pHba->pDev->irq;
  1790. HbaInfo.hbaFlags = FLG_OSD_PCI_VALID | FLG_OSD_DMA | FLG_OSD_I2O;
  1791. if(copy_to_user(argp, &HbaInfo, sizeof(HbaInfo))){
  1792. printk(KERN_WARNING"%s: Could not copy HbaInfo TO user\n",pHba->name);
  1793. return -EFAULT;
  1794. }
  1795. break;
  1796. }
  1797. case DPT_SYSINFO:
  1798. return adpt_system_info(argp);
  1799. case DPT_BLINKLED:{
  1800. u32 value;
  1801. value = (u32)adpt_read_blink_led(pHba);
  1802. if (copy_to_user(argp, &value, sizeof(value))) {
  1803. return -EFAULT;
  1804. }
  1805. break;
  1806. }
  1807. case I2ORESETCMD: {
  1808. struct Scsi_Host *shost = pHba->host;
  1809. if (shost)
  1810. spin_lock_irqsave(shost->host_lock, flags);
  1811. adpt_hba_reset(pHba);
  1812. if (shost)
  1813. spin_unlock_irqrestore(shost->host_lock, flags);
  1814. break;
  1815. }
  1816. case I2ORESCANCMD:
  1817. adpt_rescan(pHba);
  1818. break;
  1819. default:
  1820. return -EINVAL;
  1821. }
  1822. return error;
  1823. }
  1824. static long adpt_unlocked_ioctl(struct file *file, uint cmd, ulong arg)
  1825. {
  1826. struct inode *inode;
  1827. long ret;
  1828. inode = file_inode(file);
  1829. mutex_lock(&adpt_mutex);
  1830. ret = adpt_ioctl(inode, file, cmd, arg);
  1831. mutex_unlock(&adpt_mutex);
  1832. return ret;
  1833. }
  1834. #ifdef CONFIG_COMPAT
  1835. static long compat_adpt_ioctl(struct file *file,
  1836. unsigned int cmd, unsigned long arg)
  1837. {
  1838. struct inode *inode;
  1839. long ret;
  1840. inode = file_inode(file);
  1841. mutex_lock(&adpt_mutex);
  1842. switch(cmd) {
  1843. case DPT_SIGNATURE:
  1844. case I2OUSRCMD:
  1845. case DPT_CTRLINFO:
  1846. case DPT_SYSINFO:
  1847. case DPT_BLINKLED:
  1848. case I2ORESETCMD:
  1849. case I2ORESCANCMD:
  1850. case (DPT_TARGET_BUSY & 0xFFFF):
  1851. case DPT_TARGET_BUSY:
  1852. ret = adpt_ioctl(inode, file, cmd, arg);
  1853. break;
  1854. default:
  1855. ret = -ENOIOCTLCMD;
  1856. }
  1857. mutex_unlock(&adpt_mutex);
  1858. return ret;
  1859. }
  1860. #endif
  1861. static irqreturn_t adpt_isr(int irq, void *dev_id)
  1862. {
  1863. struct scsi_cmnd* cmd;
  1864. adpt_hba* pHba = dev_id;
  1865. u32 m;
  1866. void __iomem *reply;
  1867. u32 status=0;
  1868. u32 context;
  1869. ulong flags = 0;
  1870. int handled = 0;
  1871. if (pHba == NULL){
  1872. printk(KERN_WARNING"adpt_isr: NULL dev_id\n");
  1873. return IRQ_NONE;
  1874. }
  1875. if(pHba->host)
  1876. spin_lock_irqsave(pHba->host->host_lock, flags);
  1877. while( readl(pHba->irq_mask) & I2O_INTERRUPT_PENDING_B) {
  1878. m = readl(pHba->reply_port);
  1879. if(m == EMPTY_QUEUE){
  1880. // Try twice then give up
  1881. rmb();
  1882. m = readl(pHba->reply_port);
  1883. if(m == EMPTY_QUEUE){
  1884. // This really should not happen
  1885. printk(KERN_ERR"dpti: Could not get reply frame\n");
  1886. goto out;
  1887. }
  1888. }
  1889. if (pHba->reply_pool_pa <= m &&
  1890. m < pHba->reply_pool_pa +
  1891. (pHba->reply_fifo_size * REPLY_FRAME_SIZE * 4)) {
  1892. reply = (u8 *)pHba->reply_pool +
  1893. (m - pHba->reply_pool_pa);
  1894. } else {
  1895. /* Ick, we should *never* be here */
  1896. printk(KERN_ERR "dpti: reply frame not from pool\n");
  1897. reply = (u8 *)bus_to_virt(m);
  1898. }
  1899. if (readl(reply) & MSG_FAIL) {
  1900. u32 old_m = readl(reply+28);
  1901. void __iomem *msg;
  1902. u32 old_context;
  1903. PDEBUG("%s: Failed message\n",pHba->name);
  1904. if(old_m >= 0x100000){
  1905. printk(KERN_ERR"%s: Bad preserved MFA (%x)- dropping frame\n",pHba->name,old_m);
  1906. writel(m,pHba->reply_port);
  1907. continue;
  1908. }
  1909. // Transaction context is 0 in failed reply frame
  1910. msg = pHba->msg_addr_virt + old_m;
  1911. old_context = readl(msg+12);
  1912. writel(old_context, reply+12);
  1913. adpt_send_nop(pHba, old_m);
  1914. }
  1915. context = readl(reply+8);
  1916. if(context & 0x40000000){ // IOCTL
  1917. void *p = adpt_ioctl_from_context(pHba, readl(reply+12));
  1918. if( p != NULL) {
  1919. memcpy_fromio(p, reply, REPLY_FRAME_SIZE * 4);
  1920. }
  1921. // All IOCTLs will also be post wait
  1922. }
  1923. if(context & 0x80000000){ // Post wait message
  1924. status = readl(reply+16);
  1925. if(status >> 24){
  1926. status &= 0xffff; /* Get detail status */
  1927. } else {
  1928. status = I2O_POST_WAIT_OK;
  1929. }
  1930. if(!(context & 0x40000000)) {
  1931. cmd = adpt_cmd_from_context(pHba,
  1932. readl(reply+12));
  1933. if(cmd != NULL) {
  1934. printk(KERN_WARNING"%s: Apparent SCSI cmd in Post Wait Context - cmd=%p context=%x\n", pHba->name, cmd, context);
  1935. }
  1936. }
  1937. adpt_i2o_post_wait_complete(context, status);
  1938. } else { // SCSI message
  1939. cmd = adpt_cmd_from_context (pHba, readl(reply+12));
  1940. if(cmd != NULL){
  1941. scsi_dma_unmap(cmd);
  1942. if(cmd->serial_number != 0) { // If not timedout
  1943. adpt_i2o_to_scsi(reply, cmd);
  1944. }
  1945. }
  1946. }
  1947. writel(m, pHba->reply_port);
  1948. wmb();
  1949. rmb();
  1950. }
  1951. handled = 1;
  1952. out: if(pHba->host)
  1953. spin_unlock_irqrestore(pHba->host->host_lock, flags);
  1954. return IRQ_RETVAL(handled);
  1955. }
  1956. static s32 adpt_scsi_to_i2o(adpt_hba* pHba, struct scsi_cmnd* cmd, struct adpt_device* d)
  1957. {
  1958. int i;
  1959. u32 msg[MAX_MESSAGE_SIZE];
  1960. u32* mptr;
  1961. u32* lptr;
  1962. u32 *lenptr;
  1963. int direction;
  1964. int scsidir;
  1965. int nseg;
  1966. u32 len;
  1967. u32 reqlen;
  1968. s32 rcode;
  1969. dma_addr_t addr;
  1970. memset(msg, 0 , sizeof(msg));
  1971. len = scsi_bufflen(cmd);
  1972. direction = 0x00000000;
  1973. scsidir = 0x00000000; // DATA NO XFER
  1974. if(len) {
  1975. /*
  1976. * Set SCBFlags to indicate if data is being transferred
  1977. * in or out, or no data transfer
  1978. * Note: Do not have to verify index is less than 0 since
  1979. * cmd->cmnd[0] is an unsigned char
  1980. */
  1981. switch(cmd->sc_data_direction){
  1982. case DMA_FROM_DEVICE:
  1983. scsidir =0x40000000; // DATA IN (iop<--dev)
  1984. break;
  1985. case DMA_TO_DEVICE:
  1986. direction=0x04000000; // SGL OUT
  1987. scsidir =0x80000000; // DATA OUT (iop-->dev)
  1988. break;
  1989. case DMA_NONE:
  1990. break;
  1991. case DMA_BIDIRECTIONAL:
  1992. scsidir =0x40000000; // DATA IN (iop<--dev)
  1993. // Assume In - and continue;
  1994. break;
  1995. default:
  1996. printk(KERN_WARNING"%s: scsi opcode 0x%x not supported.\n",
  1997. pHba->name, cmd->cmnd[0]);
  1998. cmd->result = (DID_OK <<16) | (INITIATOR_ERROR << 8);
  1999. cmd->scsi_done(cmd);
  2000. return 0;
  2001. }
  2002. }
  2003. // msg[0] is set later
  2004. // I2O_CMD_SCSI_EXEC
  2005. msg[1] = ((0xff<<24)|(HOST_TID<<12)|d->tid);
  2006. msg[2] = 0;
  2007. msg[3] = adpt_cmd_to_context(cmd); /* Want SCSI control block back */
  2008. // Our cards use the transaction context as the tag for queueing
  2009. // Adaptec/DPT Private stuff
  2010. msg[4] = I2O_CMD_SCSI_EXEC|(DPT_ORGANIZATION_ID<<16);
  2011. msg[5] = d->tid;
  2012. /* Direction, disconnect ok | sense data | simple queue , CDBLen */
  2013. // I2O_SCB_FLAG_ENABLE_DISCONNECT |
  2014. // I2O_SCB_FLAG_SIMPLE_QUEUE_TAG |
  2015. // I2O_SCB_FLAG_SENSE_DATA_IN_MESSAGE;
  2016. msg[6] = scsidir|0x20a00000|cmd->cmd_len;
  2017. mptr=msg+7;
  2018. // Write SCSI command into the message - always 16 byte block
  2019. memset(mptr, 0, 16);
  2020. memcpy(mptr, cmd->cmnd, cmd->cmd_len);
  2021. mptr+=4;
  2022. lenptr=mptr++; /* Remember me - fill in when we know */
  2023. if (dpt_dma64(pHba)) {
  2024. reqlen = 16; // SINGLE SGE
  2025. *mptr++ = (0x7C<<24)+(2<<16)+0x02; /* Enable 64 bit */
  2026. *mptr++ = 1 << PAGE_SHIFT;
  2027. } else {
  2028. reqlen = 14; // SINGLE SGE
  2029. }
  2030. /* Now fill in the SGList and command */
  2031. nseg = scsi_dma_map(cmd);
  2032. BUG_ON(nseg < 0);
  2033. if (nseg) {
  2034. struct scatterlist *sg;
  2035. len = 0;
  2036. scsi_for_each_sg(cmd, sg, nseg, i) {
  2037. lptr = mptr;
  2038. *mptr++ = direction|0x10000000|sg_dma_len(sg);
  2039. len+=sg_dma_len(sg);
  2040. addr = sg_dma_address(sg);
  2041. *mptr++ = dma_low(addr);
  2042. if (dpt_dma64(pHba))
  2043. *mptr++ = dma_high(addr);
  2044. /* Make this an end of list */
  2045. if (i == nseg - 1)
  2046. *lptr = direction|0xD0000000|sg_dma_len(sg);
  2047. }
  2048. reqlen = mptr - msg;
  2049. *lenptr = len;
  2050. if(cmd->underflow && len != cmd->underflow){
  2051. printk(KERN_WARNING"Cmd len %08X Cmd underflow %08X\n",
  2052. len, cmd->underflow);
  2053. }
  2054. } else {
  2055. *lenptr = len = 0;
  2056. reqlen = 12;
  2057. }
  2058. /* Stick the headers on */
  2059. msg[0] = reqlen<<16 | ((reqlen > 12) ? SGL_OFFSET_12 : SGL_OFFSET_0);
  2060. // Send it on it's way
  2061. rcode = adpt_i2o_post_this(pHba, msg, reqlen<<2);
  2062. if (rcode == 0) {
  2063. return 0;
  2064. }
  2065. return rcode;
  2066. }
  2067. static s32 adpt_scsi_host_alloc(adpt_hba* pHba, struct scsi_host_template *sht)
  2068. {
  2069. struct Scsi_Host *host;
  2070. host = scsi_host_alloc(sht, sizeof(adpt_hba*));
  2071. if (host == NULL) {
  2072. printk("%s: scsi_host_alloc returned NULL\n", pHba->name);
  2073. return -1;
  2074. }
  2075. host->hostdata[0] = (unsigned long)pHba;
  2076. pHba->host = host;
  2077. host->irq = pHba->pDev->irq;
  2078. /* no IO ports, so don't have to set host->io_port and
  2079. * host->n_io_port
  2080. */
  2081. host->io_port = 0;
  2082. host->n_io_port = 0;
  2083. /* see comments in scsi_host.h */
  2084. host->max_id = 16;
  2085. host->max_lun = 256;
  2086. host->max_channel = pHba->top_scsi_channel + 1;
  2087. host->cmd_per_lun = 1;
  2088. host->unique_id = (u32)sys_tbl_pa + pHba->unit;
  2089. host->sg_tablesize = pHba->sg_tablesize;
  2090. host->can_queue = pHba->post_fifo_size;
  2091. host->use_cmd_list = 1;
  2092. return 0;
  2093. }
  2094. static s32 adpt_i2o_to_scsi(void __iomem *reply, struct scsi_cmnd* cmd)
  2095. {
  2096. adpt_hba* pHba;
  2097. u32 hba_status;
  2098. u32 dev_status;
  2099. u32 reply_flags = readl(reply) & 0xff00; // Leave it shifted up 8 bits
  2100. // I know this would look cleaner if I just read bytes
  2101. // but the model I have been using for all the rest of the
  2102. // io is in 4 byte words - so I keep that model
  2103. u16 detailed_status = readl(reply+16) &0xffff;
  2104. dev_status = (detailed_status & 0xff);
  2105. hba_status = detailed_status >> 8;
  2106. // calculate resid for sg
  2107. scsi_set_resid(cmd, scsi_bufflen(cmd) - readl(reply+20));
  2108. pHba = (adpt_hba*) cmd->device->host->hostdata[0];
  2109. cmd->sense_buffer[0] = '\0'; // initialize sense valid flag to false
  2110. if(!(reply_flags & MSG_FAIL)) {
  2111. switch(detailed_status & I2O_SCSI_DSC_MASK) {
  2112. case I2O_SCSI_DSC_SUCCESS:
  2113. cmd->result = (DID_OK << 16);
  2114. // handle underflow
  2115. if (readl(reply+20) < cmd->underflow) {
  2116. cmd->result = (DID_ERROR <<16);
  2117. printk(KERN_WARNING"%s: SCSI CMD underflow\n",pHba->name);
  2118. }
  2119. break;
  2120. case I2O_SCSI_DSC_REQUEST_ABORTED:
  2121. cmd->result = (DID_ABORT << 16);
  2122. break;
  2123. case I2O_SCSI_DSC_PATH_INVALID:
  2124. case I2O_SCSI_DSC_DEVICE_NOT_PRESENT:
  2125. case I2O_SCSI_DSC_SELECTION_TIMEOUT:
  2126. case I2O_SCSI_DSC_COMMAND_TIMEOUT:
  2127. case I2O_SCSI_DSC_NO_ADAPTER:
  2128. case I2O_SCSI_DSC_RESOURCE_UNAVAILABLE:
  2129. printk(KERN_WARNING"%s: SCSI Timeout-Device (%d,%d,%llu) hba status=0x%x, dev status=0x%x, cmd=0x%x\n",
  2130. pHba->name, (u32)cmd->device->channel, (u32)cmd->device->id, cmd->device->lun, hba_status, dev_status, cmd->cmnd[0]);
  2131. cmd->result = (DID_TIME_OUT << 16);
  2132. break;
  2133. case I2O_SCSI_DSC_ADAPTER_BUSY:
  2134. case I2O_SCSI_DSC_BUS_BUSY:
  2135. cmd->result = (DID_BUS_BUSY << 16);
  2136. break;
  2137. case I2O_SCSI_DSC_SCSI_BUS_RESET:
  2138. case I2O_SCSI_DSC_BDR_MESSAGE_SENT:
  2139. cmd->result = (DID_RESET << 16);
  2140. break;
  2141. case I2O_SCSI_DSC_PARITY_ERROR_FAILURE:
  2142. printk(KERN_WARNING"%s: SCSI CMD parity error\n",pHba->name);
  2143. cmd->result = (DID_PARITY << 16);
  2144. break;
  2145. case I2O_SCSI_DSC_UNABLE_TO_ABORT:
  2146. case I2O_SCSI_DSC_COMPLETE_WITH_ERROR:
  2147. case I2O_SCSI_DSC_UNABLE_TO_TERMINATE:
  2148. case I2O_SCSI_DSC_MR_MESSAGE_RECEIVED:
  2149. case I2O_SCSI_DSC_AUTOSENSE_FAILED:
  2150. case I2O_SCSI_DSC_DATA_OVERRUN:
  2151. case I2O_SCSI_DSC_UNEXPECTED_BUS_FREE:
  2152. case I2O_SCSI_DSC_SEQUENCE_FAILURE:
  2153. case I2O_SCSI_DSC_REQUEST_LENGTH_ERROR:
  2154. case I2O_SCSI_DSC_PROVIDE_FAILURE:
  2155. case I2O_SCSI_DSC_REQUEST_TERMINATED:
  2156. case I2O_SCSI_DSC_IDE_MESSAGE_SENT:
  2157. case I2O_SCSI_DSC_UNACKNOWLEDGED_EVENT:
  2158. case I2O_SCSI_DSC_MESSAGE_RECEIVED:
  2159. case I2O_SCSI_DSC_INVALID_CDB:
  2160. case I2O_SCSI_DSC_LUN_INVALID:
  2161. case I2O_SCSI_DSC_SCSI_TID_INVALID:
  2162. case I2O_SCSI_DSC_FUNCTION_UNAVAILABLE:
  2163. case I2O_SCSI_DSC_NO_NEXUS:
  2164. case I2O_SCSI_DSC_CDB_RECEIVED:
  2165. case I2O_SCSI_DSC_LUN_ALREADY_ENABLED:
  2166. case I2O_SCSI_DSC_QUEUE_FROZEN:
  2167. case I2O_SCSI_DSC_REQUEST_INVALID:
  2168. default:
  2169. printk(KERN_WARNING"%s: SCSI error %0x-Device(%d,%d,%llu) hba_status=0x%x, dev_status=0x%x, cmd=0x%x\n",
  2170. pHba->name, detailed_status & I2O_SCSI_DSC_MASK, (u32)cmd->device->channel, (u32)cmd->device->id, cmd->device->lun,
  2171. hba_status, dev_status, cmd->cmnd[0]);
  2172. cmd->result = (DID_ERROR << 16);
  2173. break;
  2174. }
  2175. // copy over the request sense data if it was a check
  2176. // condition status
  2177. if (dev_status == SAM_STAT_CHECK_CONDITION) {
  2178. u32 len = min(SCSI_SENSE_BUFFERSIZE, 40);
  2179. // Copy over the sense data
  2180. memcpy_fromio(cmd->sense_buffer, (reply+28) , len);
  2181. if(cmd->sense_buffer[0] == 0x70 /* class 7 */ &&
  2182. cmd->sense_buffer[2] == DATA_PROTECT ){
  2183. /* This is to handle an array failed */
  2184. cmd->result = (DID_TIME_OUT << 16);
  2185. printk(KERN_WARNING"%s: SCSI Data Protect-Device (%d,%d,%llu) hba_status=0x%x, dev_status=0x%x, cmd=0x%x\n",
  2186. pHba->name, (u32)cmd->device->channel, (u32)cmd->device->id, cmd->device->lun,
  2187. hba_status, dev_status, cmd->cmnd[0]);
  2188. }
  2189. }
  2190. } else {
  2191. /* In this condtion we could not talk to the tid
  2192. * the card rejected it. We should signal a retry
  2193. * for a limitted number of retries.
  2194. */
  2195. cmd->result = (DID_TIME_OUT << 16);
  2196. printk(KERN_WARNING"%s: I2O MSG_FAIL - Device (%d,%d,%llu) tid=%d, cmd=0x%x\n",
  2197. pHba->name, (u32)cmd->device->channel, (u32)cmd->device->id, cmd->device->lun,
  2198. ((struct adpt_device*)(cmd->device->hostdata))->tid, cmd->cmnd[0]);
  2199. }
  2200. cmd->result |= (dev_status);
  2201. if(cmd->scsi_done != NULL){
  2202. cmd->scsi_done(cmd);
  2203. }
  2204. return cmd->result;
  2205. }
  2206. static s32 adpt_rescan(adpt_hba* pHba)
  2207. {
  2208. s32 rcode;
  2209. ulong flags = 0;
  2210. if(pHba->host)
  2211. spin_lock_irqsave(pHba->host->host_lock, flags);
  2212. if ((rcode=adpt_i2o_lct_get(pHba)) < 0)
  2213. goto out;
  2214. if ((rcode=adpt_i2o_reparse_lct(pHba)) < 0)
  2215. goto out;
  2216. rcode = 0;
  2217. out: if(pHba->host)
  2218. spin_unlock_irqrestore(pHba->host->host_lock, flags);
  2219. return rcode;
  2220. }
  2221. static s32 adpt_i2o_reparse_lct(adpt_hba* pHba)
  2222. {
  2223. int i;
  2224. int max;
  2225. int tid;
  2226. struct i2o_device *d;
  2227. i2o_lct *lct = pHba->lct;
  2228. u8 bus_no = 0;
  2229. s16 scsi_id;
  2230. u64 scsi_lun;
  2231. u32 buf[10]; // at least 8 u32's
  2232. struct adpt_device* pDev = NULL;
  2233. struct i2o_device* pI2o_dev = NULL;
  2234. if (lct == NULL) {
  2235. printk(KERN_ERR "%s: LCT is empty???\n",pHba->name);
  2236. return -1;
  2237. }
  2238. max = lct->table_size;
  2239. max -= 3;
  2240. max /= 9;
  2241. // Mark each drive as unscanned
  2242. for (d = pHba->devices; d; d = d->next) {
  2243. pDev =(struct adpt_device*) d->owner;
  2244. if(!pDev){
  2245. continue;
  2246. }
  2247. pDev->state |= DPTI_DEV_UNSCANNED;
  2248. }
  2249. printk(KERN_INFO "%s: LCT has %d entries.\n", pHba->name,max);
  2250. for(i=0;i<max;i++) {
  2251. if( lct->lct_entry[i].user_tid != 0xfff){
  2252. continue;
  2253. }
  2254. if( lct->lct_entry[i].class_id == I2O_CLASS_RANDOM_BLOCK_STORAGE ||
  2255. lct->lct_entry[i].class_id == I2O_CLASS_SCSI_PERIPHERAL ||
  2256. lct->lct_entry[i].class_id == I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL ){
  2257. tid = lct->lct_entry[i].tid;
  2258. if(adpt_i2o_query_scalar(pHba, tid, 0x8000, -1, buf, 32)<0) {
  2259. printk(KERN_ERR"%s: Could not query device\n",pHba->name);
  2260. continue;
  2261. }
  2262. bus_no = buf[0]>>16;
  2263. if (bus_no >= MAX_CHANNEL) { /* Something wrong skip it */
  2264. printk(KERN_WARNING
  2265. "%s: Channel number %d out of range\n",
  2266. pHba->name, bus_no);
  2267. continue;
  2268. }
  2269. scsi_id = buf[1];
  2270. scsi_lun = scsilun_to_int((struct scsi_lun *)&buf[2]);
  2271. pDev = pHba->channel[bus_no].device[scsi_id];
  2272. /* da lun */
  2273. while(pDev) {
  2274. if(pDev->scsi_lun == scsi_lun) {
  2275. break;
  2276. }
  2277. pDev = pDev->next_lun;
  2278. }
  2279. if(!pDev ) { // Something new add it
  2280. d = kmalloc(sizeof(struct i2o_device),
  2281. GFP_ATOMIC);
  2282. if(d==NULL)
  2283. {
  2284. printk(KERN_CRIT "Out of memory for I2O device data.\n");
  2285. return -ENOMEM;
  2286. }
  2287. d->controller = pHba;
  2288. d->next = NULL;
  2289. memcpy(&d->lct_data, &lct->lct_entry[i], sizeof(i2o_lct_entry));
  2290. d->flags = 0;
  2291. adpt_i2o_report_hba_unit(pHba, d);
  2292. adpt_i2o_install_device(pHba, d);
  2293. pDev = pHba->channel[bus_no].device[scsi_id];
  2294. if( pDev == NULL){
  2295. pDev =
  2296. kzalloc(sizeof(struct adpt_device),
  2297. GFP_ATOMIC);
  2298. if(pDev == NULL) {
  2299. return -ENOMEM;
  2300. }
  2301. pHba->channel[bus_no].device[scsi_id] = pDev;
  2302. } else {
  2303. while (pDev->next_lun) {
  2304. pDev = pDev->next_lun;
  2305. }
  2306. pDev = pDev->next_lun =
  2307. kzalloc(sizeof(struct adpt_device),
  2308. GFP_ATOMIC);
  2309. if(pDev == NULL) {
  2310. return -ENOMEM;
  2311. }
  2312. }
  2313. pDev->tid = d->lct_data.tid;
  2314. pDev->scsi_channel = bus_no;
  2315. pDev->scsi_id = scsi_id;
  2316. pDev->scsi_lun = scsi_lun;
  2317. pDev->pI2o_dev = d;
  2318. d->owner = pDev;
  2319. pDev->type = (buf[0])&0xff;
  2320. pDev->flags = (buf[0]>>8)&0xff;
  2321. // Too late, SCSI system has made up it's mind, but what the hey ...
  2322. if(scsi_id > pHba->top_scsi_id){
  2323. pHba->top_scsi_id = scsi_id;
  2324. }
  2325. if(scsi_lun > pHba->top_scsi_lun){
  2326. pHba->top_scsi_lun = scsi_lun;
  2327. }
  2328. continue;
  2329. } // end of new i2o device
  2330. // We found an old device - check it
  2331. while(pDev) {
  2332. if(pDev->scsi_lun == scsi_lun) {
  2333. if(!scsi_device_online(pDev->pScsi_dev)) {
  2334. printk(KERN_WARNING"%s: Setting device (%d,%d,%llu) back online\n",
  2335. pHba->name,bus_no,scsi_id,scsi_lun);
  2336. if (pDev->pScsi_dev) {
  2337. scsi_device_set_state(pDev->pScsi_dev, SDEV_RUNNING);
  2338. }
  2339. }
  2340. d = pDev->pI2o_dev;
  2341. if(d->lct_data.tid != tid) { // something changed
  2342. pDev->tid = tid;
  2343. memcpy(&d->lct_data, &lct->lct_entry[i], sizeof(i2o_lct_entry));
  2344. if (pDev->pScsi_dev) {
  2345. pDev->pScsi_dev->changed = TRUE;
  2346. pDev->pScsi_dev->removable = TRUE;
  2347. }
  2348. }
  2349. // Found it - mark it scanned
  2350. pDev->state = DPTI_DEV_ONLINE;
  2351. break;
  2352. }
  2353. pDev = pDev->next_lun;
  2354. }
  2355. }
  2356. }
  2357. for (pI2o_dev = pHba->devices; pI2o_dev; pI2o_dev = pI2o_dev->next) {
  2358. pDev =(struct adpt_device*) pI2o_dev->owner;
  2359. if(!pDev){
  2360. continue;
  2361. }
  2362. // Drive offline drives that previously existed but could not be found
  2363. // in the LCT table
  2364. if (pDev->state & DPTI_DEV_UNSCANNED){
  2365. pDev->state = DPTI_DEV_OFFLINE;
  2366. printk(KERN_WARNING"%s: Device (%d,%d,%llu) offline\n",pHba->name,pDev->scsi_channel,pDev->scsi_id,pDev->scsi_lun);
  2367. if (pDev->pScsi_dev) {
  2368. scsi_device_set_state(pDev->pScsi_dev, SDEV_OFFLINE);
  2369. }
  2370. }
  2371. }
  2372. return 0;
  2373. }
  2374. static void adpt_fail_posted_scbs(adpt_hba* pHba)
  2375. {
  2376. struct scsi_cmnd* cmd = NULL;
  2377. struct scsi_device* d = NULL;
  2378. shost_for_each_device(d, pHba->host) {
  2379. unsigned long flags;
  2380. spin_lock_irqsave(&d->list_lock, flags);
  2381. list_for_each_entry(cmd, &d->cmd_list, list) {
  2382. if(cmd->serial_number == 0){
  2383. continue;
  2384. }
  2385. cmd->result = (DID_OK << 16) | (QUEUE_FULL <<1);
  2386. cmd->scsi_done(cmd);
  2387. }
  2388. spin_unlock_irqrestore(&d->list_lock, flags);
  2389. }
  2390. }
  2391. /*============================================================================
  2392. * Routines from i2o subsystem
  2393. *============================================================================
  2394. */
  2395. /*
  2396. * Bring an I2O controller into HOLD state. See the spec.
  2397. */
  2398. static int adpt_i2o_activate_hba(adpt_hba* pHba)
  2399. {
  2400. int rcode;
  2401. if(pHba->initialized ) {
  2402. if (adpt_i2o_status_get(pHba) < 0) {
  2403. if((rcode = adpt_i2o_reset_hba(pHba)) != 0){
  2404. printk(KERN_WARNING"%s: Could NOT reset.\n", pHba->name);
  2405. return rcode;
  2406. }
  2407. if (adpt_i2o_status_get(pHba) < 0) {
  2408. printk(KERN_INFO "HBA not responding.\n");
  2409. return -1;
  2410. }
  2411. }
  2412. if(pHba->status_block->iop_state == ADAPTER_STATE_FAULTED) {
  2413. printk(KERN_CRIT "%s: hardware fault\n", pHba->name);
  2414. return -1;
  2415. }
  2416. if (pHba->status_block->iop_state == ADAPTER_STATE_READY ||
  2417. pHba->status_block->iop_state == ADAPTER_STATE_OPERATIONAL ||
  2418. pHba->status_block->iop_state == ADAPTER_STATE_HOLD ||
  2419. pHba->status_block->iop_state == ADAPTER_STATE_FAILED) {
  2420. adpt_i2o_reset_hba(pHba);
  2421. if (adpt_i2o_status_get(pHba) < 0 || pHba->status_block->iop_state != ADAPTER_STATE_RESET) {
  2422. printk(KERN_ERR "%s: Failed to initialize.\n", pHba->name);
  2423. return -1;
  2424. }
  2425. }
  2426. } else {
  2427. if((rcode = adpt_i2o_reset_hba(pHba)) != 0){
  2428. printk(KERN_WARNING"%s: Could NOT reset.\n", pHba->name);
  2429. return rcode;
  2430. }
  2431. }
  2432. if (adpt_i2o_init_outbound_q(pHba) < 0) {
  2433. return -1;
  2434. }
  2435. /* In HOLD state */
  2436. if (adpt_i2o_hrt_get(pHba) < 0) {
  2437. return -1;
  2438. }
  2439. return 0;
  2440. }
  2441. /*
  2442. * Bring a controller online into OPERATIONAL state.
  2443. */
  2444. static int adpt_i2o_online_hba(adpt_hba* pHba)
  2445. {
  2446. if (adpt_i2o_systab_send(pHba) < 0)
  2447. return -1;
  2448. /* In READY state */
  2449. if (adpt_i2o_enable_hba(pHba) < 0)
  2450. return -1;
  2451. /* In OPERATIONAL state */
  2452. return 0;
  2453. }
  2454. static s32 adpt_send_nop(adpt_hba*pHba,u32 m)
  2455. {
  2456. u32 __iomem *msg;
  2457. ulong timeout = jiffies + 5*HZ;
  2458. while(m == EMPTY_QUEUE){
  2459. rmb();
  2460. m = readl(pHba->post_port);
  2461. if(m != EMPTY_QUEUE){
  2462. break;
  2463. }
  2464. if(time_after(jiffies,timeout)){
  2465. printk(KERN_ERR "%s: Timeout waiting for message frame!\n",pHba->name);
  2466. return 2;
  2467. }
  2468. schedule_timeout_uninterruptible(1);
  2469. }
  2470. msg = (u32 __iomem *)(pHba->msg_addr_virt + m);
  2471. writel( THREE_WORD_MSG_SIZE | SGL_OFFSET_0,&msg[0]);
  2472. writel( I2O_CMD_UTIL_NOP << 24 | HOST_TID << 12 | 0,&msg[1]);
  2473. writel( 0,&msg[2]);
  2474. wmb();
  2475. writel(m, pHba->post_port);
  2476. wmb();
  2477. return 0;
  2478. }
  2479. static s32 adpt_i2o_init_outbound_q(adpt_hba* pHba)
  2480. {
  2481. u8 *status;
  2482. dma_addr_t addr;
  2483. u32 __iomem *msg = NULL;
  2484. int i;
  2485. ulong timeout = jiffies + TMOUT_INITOUTBOUND*HZ;
  2486. u32 m;
  2487. do {
  2488. rmb();
  2489. m = readl(pHba->post_port);
  2490. if (m != EMPTY_QUEUE) {
  2491. break;
  2492. }
  2493. if(time_after(jiffies,timeout)){
  2494. printk(KERN_WARNING"%s: Timeout waiting for message frame\n",pHba->name);
  2495. return -ETIMEDOUT;
  2496. }
  2497. schedule_timeout_uninterruptible(1);
  2498. } while(m == EMPTY_QUEUE);
  2499. msg=(u32 __iomem *)(pHba->msg_addr_virt+m);
  2500. status = dma_alloc_coherent(&pHba->pDev->dev, 4, &addr, GFP_KERNEL);
  2501. if (!status) {
  2502. adpt_send_nop(pHba, m);
  2503. printk(KERN_WARNING"%s: IOP reset failed - no free memory.\n",
  2504. pHba->name);
  2505. return -ENOMEM;
  2506. }
  2507. memset(status, 0, 4);
  2508. writel(EIGHT_WORD_MSG_SIZE| SGL_OFFSET_6, &msg[0]);
  2509. writel(I2O_CMD_OUTBOUND_INIT<<24 | HOST_TID<<12 | ADAPTER_TID, &msg[1]);
  2510. writel(0, &msg[2]);
  2511. writel(0x0106, &msg[3]); /* Transaction context */
  2512. writel(4096, &msg[4]); /* Host page frame size */
  2513. writel((REPLY_FRAME_SIZE)<<16|0x80, &msg[5]); /* Outbound msg frame size and Initcode */
  2514. writel(0xD0000004, &msg[6]); /* Simple SG LE, EOB */
  2515. writel((u32)addr, &msg[7]);
  2516. writel(m, pHba->post_port);
  2517. wmb();
  2518. // Wait for the reply status to come back
  2519. do {
  2520. if (*status) {
  2521. if (*status != 0x01 /*I2O_EXEC_OUTBOUND_INIT_IN_PROGRESS*/) {
  2522. break;
  2523. }
  2524. }
  2525. rmb();
  2526. if(time_after(jiffies,timeout)){
  2527. printk(KERN_WARNING"%s: Timeout Initializing\n",pHba->name);
  2528. /* We lose 4 bytes of "status" here, but we
  2529. cannot free these because controller may
  2530. awake and corrupt those bytes at any time */
  2531. /* dma_free_coherent(&pHba->pDev->dev, 4, status, addr); */
  2532. return -ETIMEDOUT;
  2533. }
  2534. schedule_timeout_uninterruptible(1);
  2535. } while (1);
  2536. // If the command was successful, fill the fifo with our reply
  2537. // message packets
  2538. if(*status != 0x04 /*I2O_EXEC_OUTBOUND_INIT_COMPLETE*/) {
  2539. dma_free_coherent(&pHba->pDev->dev, 4, status, addr);
  2540. return -2;
  2541. }
  2542. dma_free_coherent(&pHba->pDev->dev, 4, status, addr);
  2543. if(pHba->reply_pool != NULL) {
  2544. dma_free_coherent(&pHba->pDev->dev,
  2545. pHba->reply_fifo_size * REPLY_FRAME_SIZE * 4,
  2546. pHba->reply_pool, pHba->reply_pool_pa);
  2547. }
  2548. pHba->reply_pool = dma_alloc_coherent(&pHba->pDev->dev,
  2549. pHba->reply_fifo_size * REPLY_FRAME_SIZE * 4,
  2550. &pHba->reply_pool_pa, GFP_KERNEL);
  2551. if (!pHba->reply_pool) {
  2552. printk(KERN_ERR "%s: Could not allocate reply pool\n", pHba->name);
  2553. return -ENOMEM;
  2554. }
  2555. memset(pHba->reply_pool, 0 , pHba->reply_fifo_size * REPLY_FRAME_SIZE * 4);
  2556. for(i = 0; i < pHba->reply_fifo_size; i++) {
  2557. writel(pHba->reply_pool_pa + (i * REPLY_FRAME_SIZE * 4),
  2558. pHba->reply_port);
  2559. wmb();
  2560. }
  2561. adpt_i2o_status_get(pHba);
  2562. return 0;
  2563. }
  2564. /*
  2565. * I2O System Table. Contains information about
  2566. * all the IOPs in the system. Used to inform IOPs
  2567. * about each other's existence.
  2568. *
  2569. * sys_tbl_ver is the CurrentChangeIndicator that is
  2570. * used by IOPs to track changes.
  2571. */
  2572. static s32 adpt_i2o_status_get(adpt_hba* pHba)
  2573. {
  2574. ulong timeout;
  2575. u32 m;
  2576. u32 __iomem *msg;
  2577. u8 *status_block=NULL;
  2578. if(pHba->status_block == NULL) {
  2579. pHba->status_block = dma_alloc_coherent(&pHba->pDev->dev,
  2580. sizeof(i2o_status_block),
  2581. &pHba->status_block_pa, GFP_KERNEL);
  2582. if(pHba->status_block == NULL) {
  2583. printk(KERN_ERR
  2584. "dpti%d: Get Status Block failed; Out of memory. \n",
  2585. pHba->unit);
  2586. return -ENOMEM;
  2587. }
  2588. }
  2589. memset(pHba->status_block, 0, sizeof(i2o_status_block));
  2590. status_block = (u8*)(pHba->status_block);
  2591. timeout = jiffies+TMOUT_GETSTATUS*HZ;
  2592. do {
  2593. rmb();
  2594. m = readl(pHba->post_port);
  2595. if (m != EMPTY_QUEUE) {
  2596. break;
  2597. }
  2598. if(time_after(jiffies,timeout)){
  2599. printk(KERN_ERR "%s: Timeout waiting for message !\n",
  2600. pHba->name);
  2601. return -ETIMEDOUT;
  2602. }
  2603. schedule_timeout_uninterruptible(1);
  2604. } while(m==EMPTY_QUEUE);
  2605. msg=(u32 __iomem *)(pHba->msg_addr_virt+m);
  2606. writel(NINE_WORD_MSG_SIZE|SGL_OFFSET_0, &msg[0]);
  2607. writel(I2O_CMD_STATUS_GET<<24|HOST_TID<<12|ADAPTER_TID, &msg[1]);
  2608. writel(1, &msg[2]);
  2609. writel(0, &msg[3]);
  2610. writel(0, &msg[4]);
  2611. writel(0, &msg[5]);
  2612. writel( dma_low(pHba->status_block_pa), &msg[6]);
  2613. writel( dma_high(pHba->status_block_pa), &msg[7]);
  2614. writel(sizeof(i2o_status_block), &msg[8]); // 88 bytes
  2615. //post message
  2616. writel(m, pHba->post_port);
  2617. wmb();
  2618. while(status_block[87]!=0xff){
  2619. if(time_after(jiffies,timeout)){
  2620. printk(KERN_ERR"dpti%d: Get status timeout.\n",
  2621. pHba->unit);
  2622. return -ETIMEDOUT;
  2623. }
  2624. rmb();
  2625. schedule_timeout_uninterruptible(1);
  2626. }
  2627. // Set up our number of outbound and inbound messages
  2628. pHba->post_fifo_size = pHba->status_block->max_inbound_frames;
  2629. if (pHba->post_fifo_size > MAX_TO_IOP_MESSAGES) {
  2630. pHba->post_fifo_size = MAX_TO_IOP_MESSAGES;
  2631. }
  2632. pHba->reply_fifo_size = pHba->status_block->max_outbound_frames;
  2633. if (pHba->reply_fifo_size > MAX_FROM_IOP_MESSAGES) {
  2634. pHba->reply_fifo_size = MAX_FROM_IOP_MESSAGES;
  2635. }
  2636. // Calculate the Scatter Gather list size
  2637. if (dpt_dma64(pHba)) {
  2638. pHba->sg_tablesize
  2639. = ((pHba->status_block->inbound_frame_size * 4
  2640. - 14 * sizeof(u32))
  2641. / (sizeof(struct sg_simple_element) + sizeof(u32)));
  2642. } else {
  2643. pHba->sg_tablesize
  2644. = ((pHba->status_block->inbound_frame_size * 4
  2645. - 12 * sizeof(u32))
  2646. / sizeof(struct sg_simple_element));
  2647. }
  2648. if (pHba->sg_tablesize > SG_LIST_ELEMENTS) {
  2649. pHba->sg_tablesize = SG_LIST_ELEMENTS;
  2650. }
  2651. #ifdef DEBUG
  2652. printk("dpti%d: State = ",pHba->unit);
  2653. switch(pHba->status_block->iop_state) {
  2654. case 0x01:
  2655. printk("INIT\n");
  2656. break;
  2657. case 0x02:
  2658. printk("RESET\n");
  2659. break;
  2660. case 0x04:
  2661. printk("HOLD\n");
  2662. break;
  2663. case 0x05:
  2664. printk("READY\n");
  2665. break;
  2666. case 0x08:
  2667. printk("OPERATIONAL\n");
  2668. break;
  2669. case 0x10:
  2670. printk("FAILED\n");
  2671. break;
  2672. case 0x11:
  2673. printk("FAULTED\n");
  2674. break;
  2675. default:
  2676. printk("%x (unknown!!)\n",pHba->status_block->iop_state);
  2677. }
  2678. #endif
  2679. return 0;
  2680. }
  2681. /*
  2682. * Get the IOP's Logical Configuration Table
  2683. */
  2684. static int adpt_i2o_lct_get(adpt_hba* pHba)
  2685. {
  2686. u32 msg[8];
  2687. int ret;
  2688. u32 buf[16];
  2689. if ((pHba->lct_size == 0) || (pHba->lct == NULL)){
  2690. pHba->lct_size = pHba->status_block->expected_lct_size;
  2691. }
  2692. do {
  2693. if (pHba->lct == NULL) {
  2694. pHba->lct = dma_alloc_coherent(&pHba->pDev->dev,
  2695. pHba->lct_size, &pHba->lct_pa,
  2696. GFP_ATOMIC);
  2697. if(pHba->lct == NULL) {
  2698. printk(KERN_CRIT "%s: Lct Get failed. Out of memory.\n",
  2699. pHba->name);
  2700. return -ENOMEM;
  2701. }
  2702. }
  2703. memset(pHba->lct, 0, pHba->lct_size);
  2704. msg[0] = EIGHT_WORD_MSG_SIZE|SGL_OFFSET_6;
  2705. msg[1] = I2O_CMD_LCT_NOTIFY<<24 | HOST_TID<<12 | ADAPTER_TID;
  2706. msg[2] = 0;
  2707. msg[3] = 0;
  2708. msg[4] = 0xFFFFFFFF; /* All devices */
  2709. msg[5] = 0x00000000; /* Report now */
  2710. msg[6] = 0xD0000000|pHba->lct_size;
  2711. msg[7] = (u32)pHba->lct_pa;
  2712. if ((ret=adpt_i2o_post_wait(pHba, msg, sizeof(msg), 360))) {
  2713. printk(KERN_ERR "%s: LCT Get failed (status=%#10x.\n",
  2714. pHba->name, ret);
  2715. printk(KERN_ERR"Adaptec: Error Reading Hardware.\n");
  2716. return ret;
  2717. }
  2718. if ((pHba->lct->table_size << 2) > pHba->lct_size) {
  2719. pHba->lct_size = pHba->lct->table_size << 2;
  2720. dma_free_coherent(&pHba->pDev->dev, pHba->lct_size,
  2721. pHba->lct, pHba->lct_pa);
  2722. pHba->lct = NULL;
  2723. }
  2724. } while (pHba->lct == NULL);
  2725. PDEBUG("%s: Hardware resource table read.\n", pHba->name);
  2726. // I2O_DPT_EXEC_IOP_BUFFERS_GROUP_NO;
  2727. if(adpt_i2o_query_scalar(pHba, 0 , 0x8000, -1, buf, sizeof(buf))>=0) {
  2728. pHba->FwDebugBufferSize = buf[1];
  2729. pHba->FwDebugBuffer_P = ioremap(pHba->base_addr_phys + buf[0],
  2730. pHba->FwDebugBufferSize);
  2731. if (pHba->FwDebugBuffer_P) {
  2732. pHba->FwDebugFlags_P = pHba->FwDebugBuffer_P +
  2733. FW_DEBUG_FLAGS_OFFSET;
  2734. pHba->FwDebugBLEDvalue_P = pHba->FwDebugBuffer_P +
  2735. FW_DEBUG_BLED_OFFSET;
  2736. pHba->FwDebugBLEDflag_P = pHba->FwDebugBLEDvalue_P + 1;
  2737. pHba->FwDebugStrLength_P = pHba->FwDebugBuffer_P +
  2738. FW_DEBUG_STR_LENGTH_OFFSET;
  2739. pHba->FwDebugBuffer_P += buf[2];
  2740. pHba->FwDebugFlags = 0;
  2741. }
  2742. }
  2743. return 0;
  2744. }
  2745. static int adpt_i2o_build_sys_table(void)
  2746. {
  2747. adpt_hba* pHba = hba_chain;
  2748. int count = 0;
  2749. if (sys_tbl)
  2750. dma_free_coherent(&pHba->pDev->dev, sys_tbl_len,
  2751. sys_tbl, sys_tbl_pa);
  2752. sys_tbl_len = sizeof(struct i2o_sys_tbl) + // Header + IOPs
  2753. (hba_count) * sizeof(struct i2o_sys_tbl_entry);
  2754. sys_tbl = dma_alloc_coherent(&pHba->pDev->dev,
  2755. sys_tbl_len, &sys_tbl_pa, GFP_KERNEL);
  2756. if (!sys_tbl) {
  2757. printk(KERN_WARNING "SysTab Set failed. Out of memory.\n");
  2758. return -ENOMEM;
  2759. }
  2760. memset(sys_tbl, 0, sys_tbl_len);
  2761. sys_tbl->num_entries = hba_count;
  2762. sys_tbl->version = I2OVERSION;
  2763. sys_tbl->change_ind = sys_tbl_ind++;
  2764. for(pHba = hba_chain; pHba; pHba = pHba->next) {
  2765. u64 addr;
  2766. // Get updated Status Block so we have the latest information
  2767. if (adpt_i2o_status_get(pHba)) {
  2768. sys_tbl->num_entries--;
  2769. continue; // try next one
  2770. }
  2771. sys_tbl->iops[count].org_id = pHba->status_block->org_id;
  2772. sys_tbl->iops[count].iop_id = pHba->unit + 2;
  2773. sys_tbl->iops[count].seg_num = 0;
  2774. sys_tbl->iops[count].i2o_version = pHba->status_block->i2o_version;
  2775. sys_tbl->iops[count].iop_state = pHba->status_block->iop_state;
  2776. sys_tbl->iops[count].msg_type = pHba->status_block->msg_type;
  2777. sys_tbl->iops[count].frame_size = pHba->status_block->inbound_frame_size;
  2778. sys_tbl->iops[count].last_changed = sys_tbl_ind - 1; // ??
  2779. sys_tbl->iops[count].iop_capabilities = pHba->status_block->iop_capabilities;
  2780. addr = pHba->base_addr_phys + 0x40;
  2781. sys_tbl->iops[count].inbound_low = dma_low(addr);
  2782. sys_tbl->iops[count].inbound_high = dma_high(addr);
  2783. count++;
  2784. }
  2785. #ifdef DEBUG
  2786. {
  2787. u32 *table = (u32*)sys_tbl;
  2788. printk(KERN_DEBUG"sys_tbl_len=%d in 32bit words\n",(sys_tbl_len >>2));
  2789. for(count = 0; count < (sys_tbl_len >>2); count++) {
  2790. printk(KERN_INFO "sys_tbl[%d] = %0#10x\n",
  2791. count, table[count]);
  2792. }
  2793. }
  2794. #endif
  2795. return 0;
  2796. }
  2797. /*
  2798. * Dump the information block associated with a given unit (TID)
  2799. */
  2800. static void adpt_i2o_report_hba_unit(adpt_hba* pHba, struct i2o_device *d)
  2801. {
  2802. char buf[64];
  2803. int unit = d->lct_data.tid;
  2804. printk(KERN_INFO "TID %3.3d ", unit);
  2805. if(adpt_i2o_query_scalar(pHba, unit, 0xF100, 3, buf, 16)>=0)
  2806. {
  2807. buf[16]=0;
  2808. printk(" Vendor: %-12.12s", buf);
  2809. }
  2810. if(adpt_i2o_query_scalar(pHba, unit, 0xF100, 4, buf, 16)>=0)
  2811. {
  2812. buf[16]=0;
  2813. printk(" Device: %-12.12s", buf);
  2814. }
  2815. if(adpt_i2o_query_scalar(pHba, unit, 0xF100, 6, buf, 8)>=0)
  2816. {
  2817. buf[8]=0;
  2818. printk(" Rev: %-12.12s\n", buf);
  2819. }
  2820. #ifdef DEBUG
  2821. printk(KERN_INFO "\tClass: %.21s\n", adpt_i2o_get_class_name(d->lct_data.class_id));
  2822. printk(KERN_INFO "\tSubclass: 0x%04X\n", d->lct_data.sub_class);
  2823. printk(KERN_INFO "\tFlags: ");
  2824. if(d->lct_data.device_flags&(1<<0))
  2825. printk("C"); // ConfigDialog requested
  2826. if(d->lct_data.device_flags&(1<<1))
  2827. printk("U"); // Multi-user capable
  2828. if(!(d->lct_data.device_flags&(1<<4)))
  2829. printk("P"); // Peer service enabled!
  2830. if(!(d->lct_data.device_flags&(1<<5)))
  2831. printk("M"); // Mgmt service enabled!
  2832. printk("\n");
  2833. #endif
  2834. }
  2835. #ifdef DEBUG
  2836. /*
  2837. * Do i2o class name lookup
  2838. */
  2839. static const char *adpt_i2o_get_class_name(int class)
  2840. {
  2841. int idx = 16;
  2842. static char *i2o_class_name[] = {
  2843. "Executive",
  2844. "Device Driver Module",
  2845. "Block Device",
  2846. "Tape Device",
  2847. "LAN Interface",
  2848. "WAN Interface",
  2849. "Fibre Channel Port",
  2850. "Fibre Channel Device",
  2851. "SCSI Device",
  2852. "ATE Port",
  2853. "ATE Device",
  2854. "Floppy Controller",
  2855. "Floppy Device",
  2856. "Secondary Bus Port",
  2857. "Peer Transport Agent",
  2858. "Peer Transport",
  2859. "Unknown"
  2860. };
  2861. switch(class&0xFFF) {
  2862. case I2O_CLASS_EXECUTIVE:
  2863. idx = 0; break;
  2864. case I2O_CLASS_DDM:
  2865. idx = 1; break;
  2866. case I2O_CLASS_RANDOM_BLOCK_STORAGE:
  2867. idx = 2; break;
  2868. case I2O_CLASS_SEQUENTIAL_STORAGE:
  2869. idx = 3; break;
  2870. case I2O_CLASS_LAN:
  2871. idx = 4; break;
  2872. case I2O_CLASS_WAN:
  2873. idx = 5; break;
  2874. case I2O_CLASS_FIBRE_CHANNEL_PORT:
  2875. idx = 6; break;
  2876. case I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL:
  2877. idx = 7; break;
  2878. case I2O_CLASS_SCSI_PERIPHERAL:
  2879. idx = 8; break;
  2880. case I2O_CLASS_ATE_PORT:
  2881. idx = 9; break;
  2882. case I2O_CLASS_ATE_PERIPHERAL:
  2883. idx = 10; break;
  2884. case I2O_CLASS_FLOPPY_CONTROLLER:
  2885. idx = 11; break;
  2886. case I2O_CLASS_FLOPPY_DEVICE:
  2887. idx = 12; break;
  2888. case I2O_CLASS_BUS_ADAPTER_PORT:
  2889. idx = 13; break;
  2890. case I2O_CLASS_PEER_TRANSPORT_AGENT:
  2891. idx = 14; break;
  2892. case I2O_CLASS_PEER_TRANSPORT:
  2893. idx = 15; break;
  2894. }
  2895. return i2o_class_name[idx];
  2896. }
  2897. #endif
  2898. static s32 adpt_i2o_hrt_get(adpt_hba* pHba)
  2899. {
  2900. u32 msg[6];
  2901. int ret, size = sizeof(i2o_hrt);
  2902. do {
  2903. if (pHba->hrt == NULL) {
  2904. pHba->hrt = dma_alloc_coherent(&pHba->pDev->dev,
  2905. size, &pHba->hrt_pa, GFP_KERNEL);
  2906. if (pHba->hrt == NULL) {
  2907. printk(KERN_CRIT "%s: Hrt Get failed; Out of memory.\n", pHba->name);
  2908. return -ENOMEM;
  2909. }
  2910. }
  2911. msg[0]= SIX_WORD_MSG_SIZE| SGL_OFFSET_4;
  2912. msg[1]= I2O_CMD_HRT_GET<<24 | HOST_TID<<12 | ADAPTER_TID;
  2913. msg[2]= 0;
  2914. msg[3]= 0;
  2915. msg[4]= (0xD0000000 | size); /* Simple transaction */
  2916. msg[5]= (u32)pHba->hrt_pa; /* Dump it here */
  2917. if ((ret = adpt_i2o_post_wait(pHba, msg, sizeof(msg),20))) {
  2918. printk(KERN_ERR "%s: Unable to get HRT (status=%#10x)\n", pHba->name, ret);
  2919. return ret;
  2920. }
  2921. if (pHba->hrt->num_entries * pHba->hrt->entry_len << 2 > size) {
  2922. int newsize = pHba->hrt->num_entries * pHba->hrt->entry_len << 2;
  2923. dma_free_coherent(&pHba->pDev->dev, size,
  2924. pHba->hrt, pHba->hrt_pa);
  2925. size = newsize;
  2926. pHba->hrt = NULL;
  2927. }
  2928. } while(pHba->hrt == NULL);
  2929. return 0;
  2930. }
  2931. /*
  2932. * Query one scalar group value or a whole scalar group.
  2933. */
  2934. static int adpt_i2o_query_scalar(adpt_hba* pHba, int tid,
  2935. int group, int field, void *buf, int buflen)
  2936. {
  2937. u16 opblk[] = { 1, 0, I2O_PARAMS_FIELD_GET, group, 1, field };
  2938. u8 *opblk_va;
  2939. dma_addr_t opblk_pa;
  2940. u8 *resblk_va;
  2941. dma_addr_t resblk_pa;
  2942. int size;
  2943. /* 8 bytes for header */
  2944. resblk_va = dma_alloc_coherent(&pHba->pDev->dev,
  2945. sizeof(u8) * (8 + buflen), &resblk_pa, GFP_KERNEL);
  2946. if (resblk_va == NULL) {
  2947. printk(KERN_CRIT "%s: query scalar failed; Out of memory.\n", pHba->name);
  2948. return -ENOMEM;
  2949. }
  2950. opblk_va = dma_alloc_coherent(&pHba->pDev->dev,
  2951. sizeof(opblk), &opblk_pa, GFP_KERNEL);
  2952. if (opblk_va == NULL) {
  2953. dma_free_coherent(&pHba->pDev->dev, sizeof(u8) * (8+buflen),
  2954. resblk_va, resblk_pa);
  2955. printk(KERN_CRIT "%s: query operation failed; Out of memory.\n",
  2956. pHba->name);
  2957. return -ENOMEM;
  2958. }
  2959. if (field == -1) /* whole group */
  2960. opblk[4] = -1;
  2961. memcpy(opblk_va, opblk, sizeof(opblk));
  2962. size = adpt_i2o_issue_params(I2O_CMD_UTIL_PARAMS_GET, pHba, tid,
  2963. opblk_va, opblk_pa, sizeof(opblk),
  2964. resblk_va, resblk_pa, sizeof(u8)*(8+buflen));
  2965. dma_free_coherent(&pHba->pDev->dev, sizeof(opblk), opblk_va, opblk_pa);
  2966. if (size == -ETIME) {
  2967. dma_free_coherent(&pHba->pDev->dev, sizeof(u8) * (8+buflen),
  2968. resblk_va, resblk_pa);
  2969. printk(KERN_WARNING "%s: issue params failed; Timed out.\n", pHba->name);
  2970. return -ETIME;
  2971. } else if (size == -EINTR) {
  2972. dma_free_coherent(&pHba->pDev->dev, sizeof(u8) * (8+buflen),
  2973. resblk_va, resblk_pa);
  2974. printk(KERN_WARNING "%s: issue params failed; Interrupted.\n", pHba->name);
  2975. return -EINTR;
  2976. }
  2977. memcpy(buf, resblk_va+8, buflen); /* cut off header */
  2978. dma_free_coherent(&pHba->pDev->dev, sizeof(u8) * (8+buflen),
  2979. resblk_va, resblk_pa);
  2980. if (size < 0)
  2981. return size;
  2982. return buflen;
  2983. }
  2984. /* Issue UTIL_PARAMS_GET or UTIL_PARAMS_SET
  2985. *
  2986. * This function can be used for all UtilParamsGet/Set operations.
  2987. * The OperationBlock is given in opblk-buffer,
  2988. * and results are returned in resblk-buffer.
  2989. * Note that the minimum sized resblk is 8 bytes and contains
  2990. * ResultCount, ErrorInfoSize, BlockStatus and BlockSize.
  2991. */
  2992. static int adpt_i2o_issue_params(int cmd, adpt_hba* pHba, int tid,
  2993. void *opblk_va, dma_addr_t opblk_pa, int oplen,
  2994. void *resblk_va, dma_addr_t resblk_pa, int reslen)
  2995. {
  2996. u32 msg[9];
  2997. u32 *res = (u32 *)resblk_va;
  2998. int wait_status;
  2999. msg[0] = NINE_WORD_MSG_SIZE | SGL_OFFSET_5;
  3000. msg[1] = cmd << 24 | HOST_TID << 12 | tid;
  3001. msg[2] = 0;
  3002. msg[3] = 0;
  3003. msg[4] = 0;
  3004. msg[5] = 0x54000000 | oplen; /* OperationBlock */
  3005. msg[6] = (u32)opblk_pa;
  3006. msg[7] = 0xD0000000 | reslen; /* ResultBlock */
  3007. msg[8] = (u32)resblk_pa;
  3008. if ((wait_status = adpt_i2o_post_wait(pHba, msg, sizeof(msg), 20))) {
  3009. printk("adpt_i2o_issue_params: post_wait failed (%p)\n", resblk_va);
  3010. return wait_status; /* -DetailedStatus */
  3011. }
  3012. if (res[1]&0x00FF0000) { /* BlockStatus != SUCCESS */
  3013. printk(KERN_WARNING "%s: %s - Error:\n ErrorInfoSize = 0x%02x, "
  3014. "BlockStatus = 0x%02x, BlockSize = 0x%04x\n",
  3015. pHba->name,
  3016. (cmd == I2O_CMD_UTIL_PARAMS_SET) ? "PARAMS_SET"
  3017. : "PARAMS_GET",
  3018. res[1]>>24, (res[1]>>16)&0xFF, res[1]&0xFFFF);
  3019. return -((res[1] >> 16) & 0xFF); /* -BlockStatus */
  3020. }
  3021. return 4 + ((res[1] & 0x0000FFFF) << 2); /* bytes used in resblk */
  3022. }
  3023. static s32 adpt_i2o_quiesce_hba(adpt_hba* pHba)
  3024. {
  3025. u32 msg[4];
  3026. int ret;
  3027. adpt_i2o_status_get(pHba);
  3028. /* SysQuiesce discarded if IOP not in READY or OPERATIONAL state */
  3029. if((pHba->status_block->iop_state != ADAPTER_STATE_READY) &&
  3030. (pHba->status_block->iop_state != ADAPTER_STATE_OPERATIONAL)){
  3031. return 0;
  3032. }
  3033. msg[0] = FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
  3034. msg[1] = I2O_CMD_SYS_QUIESCE<<24|HOST_TID<<12|ADAPTER_TID;
  3035. msg[2] = 0;
  3036. msg[3] = 0;
  3037. if((ret = adpt_i2o_post_wait(pHba, msg, sizeof(msg), 240))) {
  3038. printk(KERN_INFO"dpti%d: Unable to quiesce (status=%#x).\n",
  3039. pHba->unit, -ret);
  3040. } else {
  3041. printk(KERN_INFO"dpti%d: Quiesced.\n",pHba->unit);
  3042. }
  3043. adpt_i2o_status_get(pHba);
  3044. return ret;
  3045. }
  3046. /*
  3047. * Enable IOP. Allows the IOP to resume external operations.
  3048. */
  3049. static int adpt_i2o_enable_hba(adpt_hba* pHba)
  3050. {
  3051. u32 msg[4];
  3052. int ret;
  3053. adpt_i2o_status_get(pHba);
  3054. if(!pHba->status_block){
  3055. return -ENOMEM;
  3056. }
  3057. /* Enable only allowed on READY state */
  3058. if(pHba->status_block->iop_state == ADAPTER_STATE_OPERATIONAL)
  3059. return 0;
  3060. if(pHba->status_block->iop_state != ADAPTER_STATE_READY)
  3061. return -EINVAL;
  3062. msg[0]=FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
  3063. msg[1]=I2O_CMD_SYS_ENABLE<<24|HOST_TID<<12|ADAPTER_TID;
  3064. msg[2]= 0;
  3065. msg[3]= 0;
  3066. if ((ret = adpt_i2o_post_wait(pHba, msg, sizeof(msg), 240))) {
  3067. printk(KERN_WARNING"%s: Could not enable (status=%#10x).\n",
  3068. pHba->name, ret);
  3069. } else {
  3070. PDEBUG("%s: Enabled.\n", pHba->name);
  3071. }
  3072. adpt_i2o_status_get(pHba);
  3073. return ret;
  3074. }
  3075. static int adpt_i2o_systab_send(adpt_hba* pHba)
  3076. {
  3077. u32 msg[12];
  3078. int ret;
  3079. msg[0] = I2O_MESSAGE_SIZE(12) | SGL_OFFSET_6;
  3080. msg[1] = I2O_CMD_SYS_TAB_SET<<24 | HOST_TID<<12 | ADAPTER_TID;
  3081. msg[2] = 0;
  3082. msg[3] = 0;
  3083. msg[4] = (0<<16) | ((pHba->unit+2) << 12); /* Host 0 IOP ID (unit + 2) */
  3084. msg[5] = 0; /* Segment 0 */
  3085. /*
  3086. * Provide three SGL-elements:
  3087. * System table (SysTab), Private memory space declaration and
  3088. * Private i/o space declaration
  3089. */
  3090. msg[6] = 0x54000000 | sys_tbl_len;
  3091. msg[7] = (u32)sys_tbl_pa;
  3092. msg[8] = 0x54000000 | 0;
  3093. msg[9] = 0;
  3094. msg[10] = 0xD4000000 | 0;
  3095. msg[11] = 0;
  3096. if ((ret=adpt_i2o_post_wait(pHba, msg, sizeof(msg), 120))) {
  3097. printk(KERN_INFO "%s: Unable to set SysTab (status=%#10x).\n",
  3098. pHba->name, ret);
  3099. }
  3100. #ifdef DEBUG
  3101. else {
  3102. PINFO("%s: SysTab set.\n", pHba->name);
  3103. }
  3104. #endif
  3105. return ret;
  3106. }
  3107. /*============================================================================
  3108. *
  3109. *============================================================================
  3110. */
  3111. #ifdef UARTDELAY
  3112. static static void adpt_delay(int millisec)
  3113. {
  3114. int i;
  3115. for (i = 0; i < millisec; i++) {
  3116. udelay(1000); /* delay for one millisecond */
  3117. }
  3118. }
  3119. #endif
  3120. static struct scsi_host_template driver_template = {
  3121. .module = THIS_MODULE,
  3122. .name = "dpt_i2o",
  3123. .proc_name = "dpt_i2o",
  3124. .show_info = adpt_show_info,
  3125. .info = adpt_info,
  3126. .queuecommand = adpt_queue,
  3127. .eh_abort_handler = adpt_abort,
  3128. .eh_device_reset_handler = adpt_device_reset,
  3129. .eh_bus_reset_handler = adpt_bus_reset,
  3130. .eh_host_reset_handler = adpt_reset,
  3131. .bios_param = adpt_bios_param,
  3132. .slave_configure = adpt_slave_configure,
  3133. .can_queue = MAX_TO_IOP_MESSAGES,
  3134. .this_id = 7,
  3135. .use_clustering = ENABLE_CLUSTERING,
  3136. };
  3137. static int __init adpt_init(void)
  3138. {
  3139. int error;
  3140. adpt_hba *pHba, *next;
  3141. printk("Loading Adaptec I2O RAID: Version " DPT_I2O_VERSION "\n");
  3142. error = adpt_detect(&driver_template);
  3143. if (error < 0)
  3144. return error;
  3145. if (hba_chain == NULL)
  3146. return -ENODEV;
  3147. for (pHba = hba_chain; pHba; pHba = pHba->next) {
  3148. error = scsi_add_host(pHba->host, &pHba->pDev->dev);
  3149. if (error)
  3150. goto fail;
  3151. scsi_scan_host(pHba->host);
  3152. }
  3153. return 0;
  3154. fail:
  3155. for (pHba = hba_chain; pHba; pHba = next) {
  3156. next = pHba->next;
  3157. scsi_remove_host(pHba->host);
  3158. }
  3159. return error;
  3160. }
  3161. static void __exit adpt_exit(void)
  3162. {
  3163. adpt_hba *pHba, *next;
  3164. for (pHba = hba_chain; pHba; pHba = next) {
  3165. next = pHba->next;
  3166. adpt_release(pHba);
  3167. }
  3168. }
  3169. module_init(adpt_init);
  3170. module_exit(adpt_exit);
  3171. MODULE_LICENSE("GPL");