qla_os.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790
  1. /*
  2. * QLogic Fibre Channel HBA Driver
  3. * Copyright (c) 2003-2005 QLogic Corporation
  4. *
  5. * See LICENSE.qla2xxx for copyright and licensing details.
  6. */
  7. #include "qla_def.h"
  8. #include <linux/moduleparam.h>
  9. #include <linux/vmalloc.h>
  10. #include <linux/delay.h>
  11. #include <linux/kthread.h>
  12. #include <scsi/scsi_tcq.h>
  13. #include <scsi/scsicam.h>
  14. #include <scsi/scsi_transport.h>
  15. #include <scsi/scsi_transport_fc.h>
  16. /*
  17. * Driver version
  18. */
  19. char qla2x00_version_str[40];
  20. /*
  21. * SRB allocation cache
  22. */
  23. static struct kmem_cache *srb_cachep;
  24. /*
  25. * Ioctl related information.
  26. */
  27. int num_hosts;
  28. int ql2xlogintimeout = 20;
  29. module_param(ql2xlogintimeout, int, S_IRUGO|S_IRUSR);
  30. MODULE_PARM_DESC(ql2xlogintimeout,
  31. "Login timeout value in seconds.");
  32. int qlport_down_retry;
  33. module_param(qlport_down_retry, int, S_IRUGO|S_IRUSR);
  34. MODULE_PARM_DESC(qlport_down_retry,
  35. "Maximum number of command retries to a port that returns "
  36. "a PORT-DOWN status.");
  37. int ql2xplogiabsentdevice;
  38. module_param(ql2xplogiabsentdevice, int, S_IRUGO|S_IWUSR);
  39. MODULE_PARM_DESC(ql2xplogiabsentdevice,
  40. "Option to enable PLOGI to devices that are not present after "
  41. "a Fabric scan. This is needed for several broken switches. "
  42. "Default is 0 - no PLOGI. 1 - perfom PLOGI.");
  43. int ql2xloginretrycount = 0;
  44. module_param(ql2xloginretrycount, int, S_IRUGO|S_IRUSR);
  45. MODULE_PARM_DESC(ql2xloginretrycount,
  46. "Specify an alternate value for the NVRAM login retry count.");
  47. int ql2xallocfwdump = 1;
  48. module_param(ql2xallocfwdump, int, S_IRUGO|S_IRUSR);
  49. MODULE_PARM_DESC(ql2xallocfwdump,
  50. "Option to enable allocation of memory for a firmware dump "
  51. "during HBA initialization. Memory allocation requirements "
  52. "vary by ISP type. Default is 1 - allocate memory.");
  53. int ql2xextended_error_logging;
  54. module_param(ql2xextended_error_logging, int, S_IRUGO|S_IWUSR);
  55. MODULE_PARM_DESC(ql2xextended_error_logging,
  56. "Option to enable extended error logging, "
  57. "Default is 0 - no logging. 1 - log errors.");
  58. static void qla2x00_free_device(scsi_qla_host_t *);
  59. static void qla2x00_config_dma_addressing(scsi_qla_host_t *ha);
  60. int ql2xfdmienable;
  61. module_param(ql2xfdmienable, int, S_IRUGO|S_IRUSR);
  62. MODULE_PARM_DESC(ql2xfdmienable,
  63. "Enables FDMI registratons "
  64. "Default is 0 - no FDMI. 1 - perfom FDMI.");
  65. #define MAX_Q_DEPTH 32
  66. static int ql2xmaxqdepth = MAX_Q_DEPTH;
  67. module_param(ql2xmaxqdepth, int, S_IRUGO|S_IWUSR);
  68. MODULE_PARM_DESC(ql2xmaxqdepth,
  69. "Maximum queue depth to report for target devices.");
  70. int ql2xqfullrampup = 120;
  71. module_param(ql2xqfullrampup, int, S_IRUGO|S_IWUSR);
  72. MODULE_PARM_DESC(ql2xqfullrampup,
  73. "Number of seconds to wait to begin to ramp-up the queue "
  74. "depth for a device after a queue-full condition has been "
  75. "detected. Default is 120 seconds.");
  76. /*
  77. * SCSI host template entry points
  78. */
  79. static int qla2xxx_slave_configure(struct scsi_device * device);
  80. static int qla2xxx_slave_alloc(struct scsi_device *);
  81. static int qla2xxx_scan_finished(struct Scsi_Host *, unsigned long time);
  82. static void qla2xxx_scan_start(struct Scsi_Host *);
  83. static void qla2xxx_slave_destroy(struct scsi_device *);
  84. static int qla2x00_queuecommand(struct scsi_cmnd *cmd,
  85. void (*fn)(struct scsi_cmnd *));
  86. static int qla24xx_queuecommand(struct scsi_cmnd *cmd,
  87. void (*fn)(struct scsi_cmnd *));
  88. static int qla2xxx_eh_abort(struct scsi_cmnd *);
  89. static int qla2xxx_eh_device_reset(struct scsi_cmnd *);
  90. static int qla2xxx_eh_bus_reset(struct scsi_cmnd *);
  91. static int qla2xxx_eh_host_reset(struct scsi_cmnd *);
  92. static int qla2x00_loop_reset(scsi_qla_host_t *ha);
  93. static int qla2x00_device_reset(scsi_qla_host_t *, fc_port_t *);
  94. static int qla2x00_change_queue_depth(struct scsi_device *, int);
  95. static int qla2x00_change_queue_type(struct scsi_device *, int);
  96. struct scsi_host_template qla2x00_driver_template = {
  97. .module = THIS_MODULE,
  98. .name = QLA2XXX_DRIVER_NAME,
  99. .queuecommand = qla2x00_queuecommand,
  100. .eh_abort_handler = qla2xxx_eh_abort,
  101. .eh_device_reset_handler = qla2xxx_eh_device_reset,
  102. .eh_bus_reset_handler = qla2xxx_eh_bus_reset,
  103. .eh_host_reset_handler = qla2xxx_eh_host_reset,
  104. .slave_configure = qla2xxx_slave_configure,
  105. .slave_alloc = qla2xxx_slave_alloc,
  106. .slave_destroy = qla2xxx_slave_destroy,
  107. .scan_finished = qla2xxx_scan_finished,
  108. .scan_start = qla2xxx_scan_start,
  109. .change_queue_depth = qla2x00_change_queue_depth,
  110. .change_queue_type = qla2x00_change_queue_type,
  111. .this_id = -1,
  112. .cmd_per_lun = 3,
  113. .use_clustering = ENABLE_CLUSTERING,
  114. .sg_tablesize = SG_ALL,
  115. /*
  116. * The RISC allows for each command to transfer (2^32-1) bytes of data,
  117. * which equates to 0x800000 sectors.
  118. */
  119. .max_sectors = 0xFFFF,
  120. .shost_attrs = qla2x00_host_attrs,
  121. };
  122. struct scsi_host_template qla24xx_driver_template = {
  123. .module = THIS_MODULE,
  124. .name = QLA2XXX_DRIVER_NAME,
  125. .queuecommand = qla24xx_queuecommand,
  126. .eh_abort_handler = qla2xxx_eh_abort,
  127. .eh_device_reset_handler = qla2xxx_eh_device_reset,
  128. .eh_bus_reset_handler = qla2xxx_eh_bus_reset,
  129. .eh_host_reset_handler = qla2xxx_eh_host_reset,
  130. .slave_configure = qla2xxx_slave_configure,
  131. .slave_alloc = qla2xxx_slave_alloc,
  132. .slave_destroy = qla2xxx_slave_destroy,
  133. .scan_finished = qla2xxx_scan_finished,
  134. .scan_start = qla2xxx_scan_start,
  135. .change_queue_depth = qla2x00_change_queue_depth,
  136. .change_queue_type = qla2x00_change_queue_type,
  137. .this_id = -1,
  138. .cmd_per_lun = 3,
  139. .use_clustering = ENABLE_CLUSTERING,
  140. .sg_tablesize = SG_ALL,
  141. .max_sectors = 0xFFFF,
  142. .shost_attrs = qla2x00_host_attrs,
  143. };
  144. static struct scsi_transport_template *qla2xxx_transport_template = NULL;
  145. struct scsi_transport_template *qla2xxx_transport_vport_template = NULL;
  146. /* TODO Convert to inlines
  147. *
  148. * Timer routines
  149. */
  150. void qla2x00_timer(scsi_qla_host_t *);
  151. __inline__ void qla2x00_start_timer(scsi_qla_host_t *,
  152. void *, unsigned long);
  153. static __inline__ void qla2x00_restart_timer(scsi_qla_host_t *, unsigned long);
  154. __inline__ void qla2x00_stop_timer(scsi_qla_host_t *);
  155. __inline__ void
  156. qla2x00_start_timer(scsi_qla_host_t *ha, void *func, unsigned long interval)
  157. {
  158. init_timer(&ha->timer);
  159. ha->timer.expires = jiffies + interval * HZ;
  160. ha->timer.data = (unsigned long)ha;
  161. ha->timer.function = (void (*)(unsigned long))func;
  162. add_timer(&ha->timer);
  163. ha->timer_active = 1;
  164. }
  165. static inline void
  166. qla2x00_restart_timer(scsi_qla_host_t *ha, unsigned long interval)
  167. {
  168. mod_timer(&ha->timer, jiffies + interval * HZ);
  169. }
  170. __inline__ void
  171. qla2x00_stop_timer(scsi_qla_host_t *ha)
  172. {
  173. del_timer_sync(&ha->timer);
  174. ha->timer_active = 0;
  175. }
  176. static int qla2x00_do_dpc(void *data);
  177. static void qla2x00_rst_aen(scsi_qla_host_t *);
  178. uint8_t qla2x00_mem_alloc(scsi_qla_host_t *);
  179. void qla2x00_mem_free(scsi_qla_host_t *ha);
  180. static int qla2x00_allocate_sp_pool( scsi_qla_host_t *ha);
  181. static void qla2x00_free_sp_pool(scsi_qla_host_t *ha);
  182. static void qla2x00_sp_free_dma(scsi_qla_host_t *, srb_t *);
  183. void qla2x00_sp_compl(scsi_qla_host_t *ha, srb_t *);
  184. /* -------------------------------------------------------------------------- */
  185. static char *
  186. qla2x00_pci_info_str(struct scsi_qla_host *ha, char *str)
  187. {
  188. static char *pci_bus_modes[] = {
  189. "33", "66", "100", "133",
  190. };
  191. uint16_t pci_bus;
  192. strcpy(str, "PCI");
  193. pci_bus = (ha->pci_attr & (BIT_9 | BIT_10)) >> 9;
  194. if (pci_bus) {
  195. strcat(str, "-X (");
  196. strcat(str, pci_bus_modes[pci_bus]);
  197. } else {
  198. pci_bus = (ha->pci_attr & BIT_8) >> 8;
  199. strcat(str, " (");
  200. strcat(str, pci_bus_modes[pci_bus]);
  201. }
  202. strcat(str, " MHz)");
  203. return (str);
  204. }
  205. static char *
  206. qla24xx_pci_info_str(struct scsi_qla_host *ha, char *str)
  207. {
  208. static char *pci_bus_modes[] = { "33", "66", "100", "133", };
  209. uint32_t pci_bus;
  210. int pcie_reg;
  211. pcie_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_EXP);
  212. if (pcie_reg) {
  213. char lwstr[6];
  214. uint16_t pcie_lstat, lspeed, lwidth;
  215. pcie_reg += 0x12;
  216. pci_read_config_word(ha->pdev, pcie_reg, &pcie_lstat);
  217. lspeed = pcie_lstat & (BIT_0 | BIT_1 | BIT_2 | BIT_3);
  218. lwidth = (pcie_lstat &
  219. (BIT_4 | BIT_5 | BIT_6 | BIT_7 | BIT_8 | BIT_9)) >> 4;
  220. strcpy(str, "PCIe (");
  221. if (lspeed == 1)
  222. strcat(str, "2.5Gb/s ");
  223. else
  224. strcat(str, "<unknown> ");
  225. snprintf(lwstr, sizeof(lwstr), "x%d)", lwidth);
  226. strcat(str, lwstr);
  227. return str;
  228. }
  229. strcpy(str, "PCI");
  230. pci_bus = (ha->pci_attr & CSRX_PCIX_BUS_MODE_MASK) >> 8;
  231. if (pci_bus == 0 || pci_bus == 8) {
  232. strcat(str, " (");
  233. strcat(str, pci_bus_modes[pci_bus >> 3]);
  234. } else {
  235. strcat(str, "-X ");
  236. if (pci_bus & BIT_2)
  237. strcat(str, "Mode 2");
  238. else
  239. strcat(str, "Mode 1");
  240. strcat(str, " (");
  241. strcat(str, pci_bus_modes[pci_bus & ~BIT_2]);
  242. }
  243. strcat(str, " MHz)");
  244. return str;
  245. }
  246. static char *
  247. qla2x00_fw_version_str(struct scsi_qla_host *ha, char *str)
  248. {
  249. char un_str[10];
  250. sprintf(str, "%d.%02d.%02d ", ha->fw_major_version,
  251. ha->fw_minor_version,
  252. ha->fw_subminor_version);
  253. if (ha->fw_attributes & BIT_9) {
  254. strcat(str, "FLX");
  255. return (str);
  256. }
  257. switch (ha->fw_attributes & 0xFF) {
  258. case 0x7:
  259. strcat(str, "EF");
  260. break;
  261. case 0x17:
  262. strcat(str, "TP");
  263. break;
  264. case 0x37:
  265. strcat(str, "IP");
  266. break;
  267. case 0x77:
  268. strcat(str, "VI");
  269. break;
  270. default:
  271. sprintf(un_str, "(%x)", ha->fw_attributes);
  272. strcat(str, un_str);
  273. break;
  274. }
  275. if (ha->fw_attributes & 0x100)
  276. strcat(str, "X");
  277. return (str);
  278. }
  279. static char *
  280. qla24xx_fw_version_str(struct scsi_qla_host *ha, char *str)
  281. {
  282. sprintf(str, "%d.%02d.%02d ", ha->fw_major_version,
  283. ha->fw_minor_version,
  284. ha->fw_subminor_version);
  285. if (ha->fw_attributes & BIT_0)
  286. strcat(str, "[Class 2] ");
  287. if (ha->fw_attributes & BIT_1)
  288. strcat(str, "[IP] ");
  289. if (ha->fw_attributes & BIT_2)
  290. strcat(str, "[Multi-ID] ");
  291. if (ha->fw_attributes & BIT_13)
  292. strcat(str, "[Experimental]");
  293. return str;
  294. }
  295. static inline srb_t *
  296. qla2x00_get_new_sp(scsi_qla_host_t *ha, fc_port_t *fcport,
  297. struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
  298. {
  299. srb_t *sp;
  300. sp = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
  301. if (!sp)
  302. return sp;
  303. sp->ha = ha;
  304. sp->fcport = fcport;
  305. sp->cmd = cmd;
  306. sp->flags = 0;
  307. CMD_SP(cmd) = (void *)sp;
  308. cmd->scsi_done = done;
  309. return sp;
  310. }
  311. static int
  312. qla2x00_queuecommand(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
  313. {
  314. scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
  315. fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
  316. struct fc_rport *rport = starget_to_rport(scsi_target(cmd->device));
  317. srb_t *sp;
  318. int rval;
  319. rval = fc_remote_port_chkready(rport);
  320. if (rval) {
  321. cmd->result = rval;
  322. goto qc_fail_command;
  323. }
  324. /* Close window on fcport/rport state-transitioning. */
  325. if (!*(fc_port_t **)rport->dd_data) {
  326. cmd->result = DID_IMM_RETRY << 16;
  327. goto qc_fail_command;
  328. }
  329. if (atomic_read(&fcport->state) != FCS_ONLINE) {
  330. if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
  331. atomic_read(&ha->loop_state) == LOOP_DEAD) {
  332. cmd->result = DID_NO_CONNECT << 16;
  333. goto qc_fail_command;
  334. }
  335. goto qc_host_busy;
  336. }
  337. spin_unlock_irq(ha->host->host_lock);
  338. sp = qla2x00_get_new_sp(ha, fcport, cmd, done);
  339. if (!sp)
  340. goto qc_host_busy_lock;
  341. rval = qla2x00_start_scsi(sp);
  342. if (rval != QLA_SUCCESS)
  343. goto qc_host_busy_free_sp;
  344. spin_lock_irq(ha->host->host_lock);
  345. return 0;
  346. qc_host_busy_free_sp:
  347. qla2x00_sp_free_dma(ha, sp);
  348. mempool_free(sp, ha->srb_mempool);
  349. qc_host_busy_lock:
  350. spin_lock_irq(ha->host->host_lock);
  351. qc_host_busy:
  352. return SCSI_MLQUEUE_HOST_BUSY;
  353. qc_fail_command:
  354. done(cmd);
  355. return 0;
  356. }
  357. static int
  358. qla24xx_queuecommand(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
  359. {
  360. scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
  361. fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
  362. struct fc_rport *rport = starget_to_rport(scsi_target(cmd->device));
  363. srb_t *sp;
  364. int rval;
  365. scsi_qla_host_t *pha = to_qla_parent(ha);
  366. rval = fc_remote_port_chkready(rport);
  367. if (rval) {
  368. cmd->result = rval;
  369. goto qc24_fail_command;
  370. }
  371. /* Close window on fcport/rport state-transitioning. */
  372. if (!*(fc_port_t **)rport->dd_data) {
  373. cmd->result = DID_IMM_RETRY << 16;
  374. goto qc24_fail_command;
  375. }
  376. if (atomic_read(&fcport->state) != FCS_ONLINE) {
  377. if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
  378. atomic_read(&pha->loop_state) == LOOP_DEAD) {
  379. cmd->result = DID_NO_CONNECT << 16;
  380. goto qc24_fail_command;
  381. }
  382. goto qc24_host_busy;
  383. }
  384. spin_unlock_irq(ha->host->host_lock);
  385. sp = qla2x00_get_new_sp(pha, fcport, cmd, done);
  386. if (!sp)
  387. goto qc24_host_busy_lock;
  388. rval = qla24xx_start_scsi(sp);
  389. if (rval != QLA_SUCCESS)
  390. goto qc24_host_busy_free_sp;
  391. spin_lock_irq(ha->host->host_lock);
  392. return 0;
  393. qc24_host_busy_free_sp:
  394. qla2x00_sp_free_dma(pha, sp);
  395. mempool_free(sp, pha->srb_mempool);
  396. qc24_host_busy_lock:
  397. spin_lock_irq(ha->host->host_lock);
  398. qc24_host_busy:
  399. return SCSI_MLQUEUE_HOST_BUSY;
  400. qc24_fail_command:
  401. done(cmd);
  402. return 0;
  403. }
  404. /*
  405. * qla2x00_eh_wait_on_command
  406. * Waits for the command to be returned by the Firmware for some
  407. * max time.
  408. *
  409. * Input:
  410. * ha = actual ha whose done queue will contain the command
  411. * returned by firmware.
  412. * cmd = Scsi Command to wait on.
  413. * flag = Abort/Reset(Bus or Device Reset)
  414. *
  415. * Return:
  416. * Not Found : 0
  417. * Found : 1
  418. */
  419. static int
  420. qla2x00_eh_wait_on_command(scsi_qla_host_t *ha, struct scsi_cmnd *cmd)
  421. {
  422. #define ABORT_POLLING_PERIOD 1000
  423. #define ABORT_WAIT_ITER ((10 * 1000) / (ABORT_POLLING_PERIOD))
  424. unsigned long wait_iter = ABORT_WAIT_ITER;
  425. int ret = QLA_SUCCESS;
  426. while (CMD_SP(cmd)) {
  427. msleep(ABORT_POLLING_PERIOD);
  428. if (--wait_iter)
  429. break;
  430. }
  431. if (CMD_SP(cmd))
  432. ret = QLA_FUNCTION_FAILED;
  433. return ret;
  434. }
  435. /*
  436. * qla2x00_wait_for_hba_online
  437. * Wait till the HBA is online after going through
  438. * <= MAX_RETRIES_OF_ISP_ABORT or
  439. * finally HBA is disabled ie marked offline
  440. *
  441. * Input:
  442. * ha - pointer to host adapter structure
  443. *
  444. * Note:
  445. * Does context switching-Release SPIN_LOCK
  446. * (if any) before calling this routine.
  447. *
  448. * Return:
  449. * Success (Adapter is online) : 0
  450. * Failed (Adapter is offline/disabled) : 1
  451. */
  452. int
  453. qla2x00_wait_for_hba_online(scsi_qla_host_t *ha)
  454. {
  455. int return_status;
  456. unsigned long wait_online;
  457. scsi_qla_host_t *pha = to_qla_parent(ha);
  458. wait_online = jiffies + (MAX_LOOP_TIMEOUT * HZ);
  459. while (((test_bit(ISP_ABORT_NEEDED, &pha->dpc_flags)) ||
  460. test_bit(ABORT_ISP_ACTIVE, &pha->dpc_flags) ||
  461. test_bit(ISP_ABORT_RETRY, &pha->dpc_flags) ||
  462. pha->dpc_active) && time_before(jiffies, wait_online)) {
  463. msleep(1000);
  464. }
  465. if (pha->flags.online)
  466. return_status = QLA_SUCCESS;
  467. else
  468. return_status = QLA_FUNCTION_FAILED;
  469. DEBUG2(printk("%s return_status=%d\n",__func__,return_status));
  470. return (return_status);
  471. }
  472. /*
  473. * qla2x00_wait_for_loop_ready
  474. * Wait for MAX_LOOP_TIMEOUT(5 min) value for loop
  475. * to be in LOOP_READY state.
  476. * Input:
  477. * ha - pointer to host adapter structure
  478. *
  479. * Note:
  480. * Does context switching-Release SPIN_LOCK
  481. * (if any) before calling this routine.
  482. *
  483. *
  484. * Return:
  485. * Success (LOOP_READY) : 0
  486. * Failed (LOOP_NOT_READY) : 1
  487. */
  488. static inline int
  489. qla2x00_wait_for_loop_ready(scsi_qla_host_t *ha)
  490. {
  491. int return_status = QLA_SUCCESS;
  492. unsigned long loop_timeout ;
  493. scsi_qla_host_t *pha = to_qla_parent(ha);
  494. /* wait for 5 min at the max for loop to be ready */
  495. loop_timeout = jiffies + (MAX_LOOP_TIMEOUT * HZ);
  496. while ((!atomic_read(&pha->loop_down_timer) &&
  497. atomic_read(&pha->loop_state) == LOOP_DOWN) ||
  498. atomic_read(&pha->loop_state) != LOOP_READY) {
  499. if (atomic_read(&pha->loop_state) == LOOP_DEAD) {
  500. return_status = QLA_FUNCTION_FAILED;
  501. break;
  502. }
  503. msleep(1000);
  504. if (time_after_eq(jiffies, loop_timeout)) {
  505. return_status = QLA_FUNCTION_FAILED;
  506. break;
  507. }
  508. }
  509. return (return_status);
  510. }
  511. static void
  512. qla2x00_block_error_handler(struct scsi_cmnd *cmnd)
  513. {
  514. struct Scsi_Host *shost = cmnd->device->host;
  515. struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
  516. unsigned long flags;
  517. spin_lock_irqsave(shost->host_lock, flags);
  518. while (rport->port_state == FC_PORTSTATE_BLOCKED) {
  519. spin_unlock_irqrestore(shost->host_lock, flags);
  520. msleep(1000);
  521. spin_lock_irqsave(shost->host_lock, flags);
  522. }
  523. spin_unlock_irqrestore(shost->host_lock, flags);
  524. return;
  525. }
  526. /**************************************************************************
  527. * qla2xxx_eh_abort
  528. *
  529. * Description:
  530. * The abort function will abort the specified command.
  531. *
  532. * Input:
  533. * cmd = Linux SCSI command packet to be aborted.
  534. *
  535. * Returns:
  536. * Either SUCCESS or FAILED.
  537. *
  538. * Note:
  539. * Only return FAILED if command not returned by firmware.
  540. **************************************************************************/
  541. static int
  542. qla2xxx_eh_abort(struct scsi_cmnd *cmd)
  543. {
  544. scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
  545. srb_t *sp;
  546. int ret, i;
  547. unsigned int id, lun;
  548. unsigned long serial;
  549. unsigned long flags;
  550. int wait = 0;
  551. scsi_qla_host_t *pha = to_qla_parent(ha);
  552. qla2x00_block_error_handler(cmd);
  553. if (!CMD_SP(cmd))
  554. return SUCCESS;
  555. ret = SUCCESS;
  556. id = cmd->device->id;
  557. lun = cmd->device->lun;
  558. serial = cmd->serial_number;
  559. /* Check active list for command command. */
  560. spin_lock_irqsave(&pha->hardware_lock, flags);
  561. for (i = 1; i < MAX_OUTSTANDING_COMMANDS; i++) {
  562. sp = pha->outstanding_cmds[i];
  563. if (sp == NULL)
  564. continue;
  565. if (sp->cmd != cmd)
  566. continue;
  567. DEBUG2(printk("%s(%ld): aborting sp %p from RISC. pid=%ld.\n",
  568. __func__, ha->host_no, sp, serial));
  569. DEBUG3(qla2x00_print_scsi_cmd(cmd));
  570. spin_unlock_irqrestore(&pha->hardware_lock, flags);
  571. if (ha->isp_ops.abort_command(ha, sp)) {
  572. DEBUG2(printk("%s(%ld): abort_command "
  573. "mbx failed.\n", __func__, ha->host_no));
  574. } else {
  575. DEBUG3(printk("%s(%ld): abort_command "
  576. "mbx success.\n", __func__, ha->host_no));
  577. wait = 1;
  578. }
  579. spin_lock_irqsave(&pha->hardware_lock, flags);
  580. break;
  581. }
  582. spin_unlock_irqrestore(&pha->hardware_lock, flags);
  583. /* Wait for the command to be returned. */
  584. if (wait) {
  585. if (qla2x00_eh_wait_on_command(ha, cmd) != QLA_SUCCESS) {
  586. qla_printk(KERN_ERR, ha,
  587. "scsi(%ld:%d:%d): Abort handler timed out -- %lx "
  588. "%x.\n", ha->host_no, id, lun, serial, ret);
  589. ret = FAILED;
  590. }
  591. }
  592. qla_printk(KERN_INFO, ha,
  593. "scsi(%ld:%d:%d): Abort command issued -- %d %lx %x.\n",
  594. ha->host_no, id, lun, wait, serial, ret);
  595. return ret;
  596. }
  597. /**************************************************************************
  598. * qla2x00_eh_wait_for_pending_target_commands
  599. *
  600. * Description:
  601. * Waits for all the commands to come back from the specified target.
  602. *
  603. * Input:
  604. * ha - pointer to scsi_qla_host structure.
  605. * t - target
  606. * Returns:
  607. * Either SUCCESS or FAILED.
  608. *
  609. * Note:
  610. **************************************************************************/
  611. static int
  612. qla2x00_eh_wait_for_pending_target_commands(scsi_qla_host_t *ha, unsigned int t)
  613. {
  614. int cnt;
  615. int status;
  616. srb_t *sp;
  617. struct scsi_cmnd *cmd;
  618. unsigned long flags;
  619. scsi_qla_host_t *pha = to_qla_parent(ha);
  620. status = 0;
  621. /*
  622. * Waiting for all commands for the designated target in the active
  623. * array
  624. */
  625. for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
  626. spin_lock_irqsave(&pha->hardware_lock, flags);
  627. sp = pha->outstanding_cmds[cnt];
  628. if (sp) {
  629. cmd = sp->cmd;
  630. spin_unlock_irqrestore(&pha->hardware_lock, flags);
  631. if (cmd->device->id == t &&
  632. ha->vp_idx == sp->ha->vp_idx) {
  633. if (!qla2x00_eh_wait_on_command(ha, cmd)) {
  634. status = 1;
  635. break;
  636. }
  637. }
  638. } else {
  639. spin_unlock_irqrestore(&pha->hardware_lock, flags);
  640. }
  641. }
  642. return (status);
  643. }
  644. /**************************************************************************
  645. * qla2xxx_eh_device_reset
  646. *
  647. * Description:
  648. * The device reset function will reset the target and abort any
  649. * executing commands.
  650. *
  651. * NOTE: The use of SP is undefined within this context. Do *NOT*
  652. * attempt to use this value, even if you determine it is
  653. * non-null.
  654. *
  655. * Input:
  656. * cmd = Linux SCSI command packet of the command that cause the
  657. * bus device reset.
  658. *
  659. * Returns:
  660. * SUCCESS/FAILURE (defined as macro in scsi.h).
  661. *
  662. **************************************************************************/
  663. static int
  664. qla2xxx_eh_device_reset(struct scsi_cmnd *cmd)
  665. {
  666. scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
  667. fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
  668. int ret = FAILED;
  669. unsigned int id, lun;
  670. unsigned long serial;
  671. qla2x00_block_error_handler(cmd);
  672. id = cmd->device->id;
  673. lun = cmd->device->lun;
  674. serial = cmd->serial_number;
  675. if (!fcport)
  676. return ret;
  677. qla_printk(KERN_INFO, ha,
  678. "scsi(%ld:%d:%d): DEVICE RESET ISSUED.\n", ha->host_no, id, lun);
  679. if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
  680. goto eh_dev_reset_done;
  681. if (qla2x00_wait_for_loop_ready(ha) == QLA_SUCCESS) {
  682. if (qla2x00_device_reset(ha, fcport) == 0)
  683. ret = SUCCESS;
  684. #if defined(LOGOUT_AFTER_DEVICE_RESET)
  685. if (ret == SUCCESS) {
  686. if (fcport->flags & FC_FABRIC_DEVICE) {
  687. ha->isp_ops.fabric_logout(ha, fcport->loop_id);
  688. qla2x00_mark_device_lost(ha, fcport, 0, 0);
  689. }
  690. }
  691. #endif
  692. } else {
  693. DEBUG2(printk(KERN_INFO
  694. "%s failed: loop not ready\n",__func__));
  695. }
  696. if (ret == FAILED) {
  697. DEBUG3(printk("%s(%ld): device reset failed\n",
  698. __func__, ha->host_no));
  699. qla_printk(KERN_INFO, ha, "%s: device reset failed\n",
  700. __func__);
  701. goto eh_dev_reset_done;
  702. }
  703. /* Flush outstanding commands. */
  704. if (qla2x00_eh_wait_for_pending_target_commands(ha, id))
  705. ret = FAILED;
  706. if (ret == FAILED) {
  707. DEBUG3(printk("%s(%ld): failed while waiting for commands\n",
  708. __func__, ha->host_no));
  709. qla_printk(KERN_INFO, ha,
  710. "%s: failed while waiting for commands\n", __func__);
  711. } else
  712. qla_printk(KERN_INFO, ha,
  713. "scsi(%ld:%d:%d): DEVICE RESET SUCCEEDED.\n", ha->host_no,
  714. id, lun);
  715. eh_dev_reset_done:
  716. return ret;
  717. }
  718. /**************************************************************************
  719. * qla2x00_eh_wait_for_pending_commands
  720. *
  721. * Description:
  722. * Waits for all the commands to come back from the specified host.
  723. *
  724. * Input:
  725. * ha - pointer to scsi_qla_host structure.
  726. *
  727. * Returns:
  728. * 1 : SUCCESS
  729. * 0 : FAILED
  730. *
  731. * Note:
  732. **************************************************************************/
  733. static int
  734. qla2x00_eh_wait_for_pending_commands(scsi_qla_host_t *ha)
  735. {
  736. int cnt;
  737. int status;
  738. srb_t *sp;
  739. struct scsi_cmnd *cmd;
  740. unsigned long flags;
  741. status = 1;
  742. /*
  743. * Waiting for all commands for the designated target in the active
  744. * array
  745. */
  746. for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
  747. spin_lock_irqsave(&ha->hardware_lock, flags);
  748. sp = ha->outstanding_cmds[cnt];
  749. if (sp) {
  750. cmd = sp->cmd;
  751. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  752. status = qla2x00_eh_wait_on_command(ha, cmd);
  753. if (status == 0)
  754. break;
  755. }
  756. else {
  757. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  758. }
  759. }
  760. return (status);
  761. }
  762. /**************************************************************************
  763. * qla2xxx_eh_bus_reset
  764. *
  765. * Description:
  766. * The bus reset function will reset the bus and abort any executing
  767. * commands.
  768. *
  769. * Input:
  770. * cmd = Linux SCSI command packet of the command that cause the
  771. * bus reset.
  772. *
  773. * Returns:
  774. * SUCCESS/FAILURE (defined as macro in scsi.h).
  775. *
  776. **************************************************************************/
  777. static int
  778. qla2xxx_eh_bus_reset(struct scsi_cmnd *cmd)
  779. {
  780. scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
  781. scsi_qla_host_t *pha = to_qla_parent(ha);
  782. fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
  783. int ret = FAILED;
  784. unsigned int id, lun;
  785. unsigned long serial;
  786. qla2x00_block_error_handler(cmd);
  787. id = cmd->device->id;
  788. lun = cmd->device->lun;
  789. serial = cmd->serial_number;
  790. if (!fcport)
  791. return ret;
  792. qla_printk(KERN_INFO, ha,
  793. "scsi(%ld:%d:%d): LOOP RESET ISSUED.\n", ha->host_no, id, lun);
  794. if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS) {
  795. DEBUG2(printk("%s failed:board disabled\n",__func__));
  796. goto eh_bus_reset_done;
  797. }
  798. if (qla2x00_wait_for_loop_ready(ha) == QLA_SUCCESS) {
  799. if (qla2x00_loop_reset(ha) == QLA_SUCCESS)
  800. ret = SUCCESS;
  801. }
  802. if (ret == FAILED)
  803. goto eh_bus_reset_done;
  804. /* Flush outstanding commands. */
  805. if (!qla2x00_eh_wait_for_pending_commands(pha))
  806. ret = FAILED;
  807. eh_bus_reset_done:
  808. qla_printk(KERN_INFO, ha, "%s: reset %s\n", __func__,
  809. (ret == FAILED) ? "failed" : "succeded");
  810. return ret;
  811. }
  812. /**************************************************************************
  813. * qla2xxx_eh_host_reset
  814. *
  815. * Description:
  816. * The reset function will reset the Adapter.
  817. *
  818. * Input:
  819. * cmd = Linux SCSI command packet of the command that cause the
  820. * adapter reset.
  821. *
  822. * Returns:
  823. * Either SUCCESS or FAILED.
  824. *
  825. * Note:
  826. **************************************************************************/
  827. static int
  828. qla2xxx_eh_host_reset(struct scsi_cmnd *cmd)
  829. {
  830. scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
  831. fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
  832. int ret = FAILED;
  833. unsigned int id, lun;
  834. unsigned long serial;
  835. scsi_qla_host_t *pha = to_qla_parent(ha);
  836. qla2x00_block_error_handler(cmd);
  837. id = cmd->device->id;
  838. lun = cmd->device->lun;
  839. serial = cmd->serial_number;
  840. if (!fcport)
  841. return ret;
  842. qla_printk(KERN_INFO, ha,
  843. "scsi(%ld:%d:%d): ADAPTER RESET ISSUED.\n", ha->host_no, id, lun);
  844. if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
  845. goto eh_host_reset_lock;
  846. /*
  847. * Fixme-may be dpc thread is active and processing
  848. * loop_resync,so wait a while for it to
  849. * be completed and then issue big hammer.Otherwise
  850. * it may cause I/O failure as big hammer marks the
  851. * devices as lost kicking of the port_down_timer
  852. * while dpc is stuck for the mailbox to complete.
  853. */
  854. qla2x00_wait_for_loop_ready(ha);
  855. set_bit(ABORT_ISP_ACTIVE, &pha->dpc_flags);
  856. if (qla2x00_abort_isp(pha)) {
  857. clear_bit(ABORT_ISP_ACTIVE, &pha->dpc_flags);
  858. /* failed. schedule dpc to try */
  859. set_bit(ISP_ABORT_NEEDED, &pha->dpc_flags);
  860. if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
  861. goto eh_host_reset_lock;
  862. }
  863. clear_bit(ABORT_ISP_ACTIVE, &pha->dpc_flags);
  864. /* Waiting for our command in done_queue to be returned to OS.*/
  865. if (qla2x00_eh_wait_for_pending_commands(pha))
  866. ret = SUCCESS;
  867. if (ha->parent)
  868. qla2x00_vp_abort_isp(ha);
  869. eh_host_reset_lock:
  870. qla_printk(KERN_INFO, ha, "%s: reset %s\n", __func__,
  871. (ret == FAILED) ? "failed" : "succeded");
  872. return ret;
  873. }
  874. /*
  875. * qla2x00_loop_reset
  876. * Issue loop reset.
  877. *
  878. * Input:
  879. * ha = adapter block pointer.
  880. *
  881. * Returns:
  882. * 0 = success
  883. */
  884. static int
  885. qla2x00_loop_reset(scsi_qla_host_t *ha)
  886. {
  887. int ret;
  888. struct fc_port *fcport;
  889. if (ha->flags.enable_lip_full_login) {
  890. ret = qla2x00_full_login_lip(ha);
  891. if (ret != QLA_SUCCESS) {
  892. DEBUG2_3(printk("%s(%ld): bus_reset failed: "
  893. "full_login_lip=%d.\n", __func__, ha->host_no,
  894. ret));
  895. }
  896. atomic_set(&ha->loop_state, LOOP_DOWN);
  897. atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
  898. qla2x00_mark_all_devices_lost(ha, 0);
  899. qla2x00_wait_for_loop_ready(ha);
  900. }
  901. if (ha->flags.enable_lip_reset) {
  902. ret = qla2x00_lip_reset(ha);
  903. if (ret != QLA_SUCCESS) {
  904. DEBUG2_3(printk("%s(%ld): bus_reset failed: "
  905. "lip_reset=%d.\n", __func__, ha->host_no, ret));
  906. }
  907. qla2x00_wait_for_loop_ready(ha);
  908. }
  909. if (ha->flags.enable_target_reset) {
  910. list_for_each_entry(fcport, &ha->fcports, list) {
  911. if (fcport->port_type != FCT_TARGET)
  912. continue;
  913. ret = qla2x00_device_reset(ha, fcport);
  914. if (ret != QLA_SUCCESS) {
  915. DEBUG2_3(printk("%s(%ld): bus_reset failed: "
  916. "target_reset=%d d_id=%x.\n", __func__,
  917. ha->host_no, ret, fcport->d_id.b24));
  918. }
  919. }
  920. }
  921. /* Issue marker command only when we are going to start the I/O */
  922. ha->marker_needed = 1;
  923. return QLA_SUCCESS;
  924. }
  925. /*
  926. * qla2x00_device_reset
  927. * Issue bus device reset message to the target.
  928. *
  929. * Input:
  930. * ha = adapter block pointer.
  931. * t = SCSI ID.
  932. * TARGET_QUEUE_LOCK must be released.
  933. * ADAPTER_STATE_LOCK must be released.
  934. *
  935. * Context:
  936. * Kernel context.
  937. */
  938. static int
  939. qla2x00_device_reset(scsi_qla_host_t *ha, fc_port_t *reset_fcport)
  940. {
  941. /* Abort Target command will clear Reservation */
  942. return ha->isp_ops.abort_target(reset_fcport);
  943. }
  944. static int
  945. qla2xxx_slave_alloc(struct scsi_device *sdev)
  946. {
  947. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  948. if (!rport || fc_remote_port_chkready(rport))
  949. return -ENXIO;
  950. sdev->hostdata = *(fc_port_t **)rport->dd_data;
  951. return 0;
  952. }
  953. static int
  954. qla2xxx_slave_configure(struct scsi_device *sdev)
  955. {
  956. scsi_qla_host_t *ha = to_qla_host(sdev->host);
  957. struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
  958. if (sdev->tagged_supported)
  959. scsi_activate_tcq(sdev, ha->max_q_depth);
  960. else
  961. scsi_deactivate_tcq(sdev, ha->max_q_depth);
  962. rport->dev_loss_tmo = ha->port_down_retry_count + 5;
  963. return 0;
  964. }
  965. static void
  966. qla2xxx_slave_destroy(struct scsi_device *sdev)
  967. {
  968. sdev->hostdata = NULL;
  969. }
  970. static int
  971. qla2x00_change_queue_depth(struct scsi_device *sdev, int qdepth)
  972. {
  973. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
  974. return sdev->queue_depth;
  975. }
  976. static int
  977. qla2x00_change_queue_type(struct scsi_device *sdev, int tag_type)
  978. {
  979. if (sdev->tagged_supported) {
  980. scsi_set_tag_type(sdev, tag_type);
  981. if (tag_type)
  982. scsi_activate_tcq(sdev, sdev->queue_depth);
  983. else
  984. scsi_deactivate_tcq(sdev, sdev->queue_depth);
  985. } else
  986. tag_type = 0;
  987. return tag_type;
  988. }
  989. /**
  990. * qla2x00_config_dma_addressing() - Configure OS DMA addressing method.
  991. * @ha: HA context
  992. *
  993. * At exit, the @ha's flags.enable_64bit_addressing set to indicated
  994. * supported addressing method.
  995. */
  996. static void
  997. qla2x00_config_dma_addressing(scsi_qla_host_t *ha)
  998. {
  999. /* Assume a 32bit DMA mask. */
  1000. ha->flags.enable_64bit_addressing = 0;
  1001. if (!dma_set_mask(&ha->pdev->dev, DMA_64BIT_MASK)) {
  1002. /* Any upper-dword bits set? */
  1003. if (MSD(dma_get_required_mask(&ha->pdev->dev)) &&
  1004. !pci_set_consistent_dma_mask(ha->pdev, DMA_64BIT_MASK)) {
  1005. /* Ok, a 64bit DMA mask is applicable. */
  1006. ha->flags.enable_64bit_addressing = 1;
  1007. ha->isp_ops.calc_req_entries = qla2x00_calc_iocbs_64;
  1008. ha->isp_ops.build_iocbs = qla2x00_build_scsi_iocbs_64;
  1009. return;
  1010. }
  1011. }
  1012. dma_set_mask(&ha->pdev->dev, DMA_32BIT_MASK);
  1013. pci_set_consistent_dma_mask(ha->pdev, DMA_32BIT_MASK);
  1014. }
  1015. static inline void
  1016. qla2x00_set_isp_flags(scsi_qla_host_t *ha)
  1017. {
  1018. ha->device_type = DT_EXTENDED_IDS;
  1019. switch (ha->pdev->device) {
  1020. case PCI_DEVICE_ID_QLOGIC_ISP2100:
  1021. ha->device_type |= DT_ISP2100;
  1022. ha->device_type &= ~DT_EXTENDED_IDS;
  1023. ha->fw_srisc_address = RISC_START_ADDRESS_2100;
  1024. break;
  1025. case PCI_DEVICE_ID_QLOGIC_ISP2200:
  1026. ha->device_type |= DT_ISP2200;
  1027. ha->device_type &= ~DT_EXTENDED_IDS;
  1028. ha->fw_srisc_address = RISC_START_ADDRESS_2100;
  1029. break;
  1030. case PCI_DEVICE_ID_QLOGIC_ISP2300:
  1031. ha->device_type |= DT_ISP2300;
  1032. ha->device_type |= DT_ZIO_SUPPORTED;
  1033. ha->fw_srisc_address = RISC_START_ADDRESS_2300;
  1034. break;
  1035. case PCI_DEVICE_ID_QLOGIC_ISP2312:
  1036. ha->device_type |= DT_ISP2312;
  1037. ha->device_type |= DT_ZIO_SUPPORTED;
  1038. ha->fw_srisc_address = RISC_START_ADDRESS_2300;
  1039. break;
  1040. case PCI_DEVICE_ID_QLOGIC_ISP2322:
  1041. ha->device_type |= DT_ISP2322;
  1042. ha->device_type |= DT_ZIO_SUPPORTED;
  1043. if (ha->pdev->subsystem_vendor == 0x1028 &&
  1044. ha->pdev->subsystem_device == 0x0170)
  1045. ha->device_type |= DT_OEM_001;
  1046. ha->fw_srisc_address = RISC_START_ADDRESS_2300;
  1047. break;
  1048. case PCI_DEVICE_ID_QLOGIC_ISP6312:
  1049. ha->device_type |= DT_ISP6312;
  1050. ha->fw_srisc_address = RISC_START_ADDRESS_2300;
  1051. break;
  1052. case PCI_DEVICE_ID_QLOGIC_ISP6322:
  1053. ha->device_type |= DT_ISP6322;
  1054. ha->fw_srisc_address = RISC_START_ADDRESS_2300;
  1055. break;
  1056. case PCI_DEVICE_ID_QLOGIC_ISP2422:
  1057. ha->device_type |= DT_ISP2422;
  1058. ha->device_type |= DT_ZIO_SUPPORTED;
  1059. ha->device_type |= DT_FWI2;
  1060. ha->fw_srisc_address = RISC_START_ADDRESS_2400;
  1061. break;
  1062. case PCI_DEVICE_ID_QLOGIC_ISP2432:
  1063. ha->device_type |= DT_ISP2432;
  1064. ha->device_type |= DT_ZIO_SUPPORTED;
  1065. ha->device_type |= DT_FWI2;
  1066. ha->fw_srisc_address = RISC_START_ADDRESS_2400;
  1067. break;
  1068. case PCI_DEVICE_ID_QLOGIC_ISP5422:
  1069. ha->device_type |= DT_ISP5422;
  1070. ha->device_type |= DT_FWI2;
  1071. ha->fw_srisc_address = RISC_START_ADDRESS_2400;
  1072. break;
  1073. case PCI_DEVICE_ID_QLOGIC_ISP5432:
  1074. ha->device_type |= DT_ISP5432;
  1075. ha->device_type |= DT_FWI2;
  1076. ha->fw_srisc_address = RISC_START_ADDRESS_2400;
  1077. break;
  1078. }
  1079. }
  1080. static int
  1081. qla2x00_iospace_config(scsi_qla_host_t *ha)
  1082. {
  1083. unsigned long pio, pio_len, pio_flags;
  1084. unsigned long mmio, mmio_len, mmio_flags;
  1085. /* We only need PIO for Flash operations on ISP2312 v2 chips. */
  1086. pio = pci_resource_start(ha->pdev, 0);
  1087. pio_len = pci_resource_len(ha->pdev, 0);
  1088. pio_flags = pci_resource_flags(ha->pdev, 0);
  1089. if (pio_flags & IORESOURCE_IO) {
  1090. if (pio_len < MIN_IOBASE_LEN) {
  1091. qla_printk(KERN_WARNING, ha,
  1092. "Invalid PCI I/O region size (%s)...\n",
  1093. pci_name(ha->pdev));
  1094. pio = 0;
  1095. }
  1096. } else {
  1097. qla_printk(KERN_WARNING, ha,
  1098. "region #0 not a PIO resource (%s)...\n",
  1099. pci_name(ha->pdev));
  1100. pio = 0;
  1101. }
  1102. /* Use MMIO operations for all accesses. */
  1103. mmio = pci_resource_start(ha->pdev, 1);
  1104. mmio_len = pci_resource_len(ha->pdev, 1);
  1105. mmio_flags = pci_resource_flags(ha->pdev, 1);
  1106. if (!(mmio_flags & IORESOURCE_MEM)) {
  1107. qla_printk(KERN_ERR, ha,
  1108. "region #0 not an MMIO resource (%s), aborting\n",
  1109. pci_name(ha->pdev));
  1110. goto iospace_error_exit;
  1111. }
  1112. if (mmio_len < MIN_IOBASE_LEN) {
  1113. qla_printk(KERN_ERR, ha,
  1114. "Invalid PCI mem region size (%s), aborting\n",
  1115. pci_name(ha->pdev));
  1116. goto iospace_error_exit;
  1117. }
  1118. if (pci_request_regions(ha->pdev, QLA2XXX_DRIVER_NAME)) {
  1119. qla_printk(KERN_WARNING, ha,
  1120. "Failed to reserve PIO/MMIO regions (%s)\n",
  1121. pci_name(ha->pdev));
  1122. goto iospace_error_exit;
  1123. }
  1124. ha->pio_address = pio;
  1125. ha->pio_length = pio_len;
  1126. ha->iobase = ioremap(mmio, MIN_IOBASE_LEN);
  1127. if (!ha->iobase) {
  1128. qla_printk(KERN_ERR, ha,
  1129. "cannot remap MMIO (%s), aborting\n", pci_name(ha->pdev));
  1130. goto iospace_error_exit;
  1131. }
  1132. return (0);
  1133. iospace_error_exit:
  1134. return (-ENOMEM);
  1135. }
  1136. static void
  1137. qla2x00_enable_intrs(scsi_qla_host_t *ha)
  1138. {
  1139. unsigned long flags = 0;
  1140. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1141. spin_lock_irqsave(&ha->hardware_lock, flags);
  1142. ha->interrupts_on = 1;
  1143. /* enable risc and host interrupts */
  1144. WRT_REG_WORD(&reg->ictrl, ICR_EN_INT | ICR_EN_RISC);
  1145. RD_REG_WORD(&reg->ictrl);
  1146. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1147. }
  1148. static void
  1149. qla2x00_disable_intrs(scsi_qla_host_t *ha)
  1150. {
  1151. unsigned long flags = 0;
  1152. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1153. spin_lock_irqsave(&ha->hardware_lock, flags);
  1154. ha->interrupts_on = 0;
  1155. /* disable risc and host interrupts */
  1156. WRT_REG_WORD(&reg->ictrl, 0);
  1157. RD_REG_WORD(&reg->ictrl);
  1158. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1159. }
  1160. static void
  1161. qla24xx_enable_intrs(scsi_qla_host_t *ha)
  1162. {
  1163. unsigned long flags = 0;
  1164. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  1165. spin_lock_irqsave(&ha->hardware_lock, flags);
  1166. ha->interrupts_on = 1;
  1167. WRT_REG_DWORD(&reg->ictrl, ICRX_EN_RISC_INT);
  1168. RD_REG_DWORD(&reg->ictrl);
  1169. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1170. }
  1171. static void
  1172. qla24xx_disable_intrs(scsi_qla_host_t *ha)
  1173. {
  1174. unsigned long flags = 0;
  1175. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  1176. spin_lock_irqsave(&ha->hardware_lock, flags);
  1177. ha->interrupts_on = 0;
  1178. WRT_REG_DWORD(&reg->ictrl, 0);
  1179. RD_REG_DWORD(&reg->ictrl);
  1180. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1181. }
  1182. static void
  1183. qla2xxx_scan_start(struct Scsi_Host *shost)
  1184. {
  1185. scsi_qla_host_t *ha = (scsi_qla_host_t *)shost->hostdata;
  1186. set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
  1187. set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
  1188. set_bit(RSCN_UPDATE, &ha->dpc_flags);
  1189. }
  1190. static int
  1191. qla2xxx_scan_finished(struct Scsi_Host *shost, unsigned long time)
  1192. {
  1193. scsi_qla_host_t *ha = (scsi_qla_host_t *)shost->hostdata;
  1194. if (!ha->host)
  1195. return 1;
  1196. if (time > ha->loop_reset_delay * HZ)
  1197. return 1;
  1198. return atomic_read(&ha->loop_state) == LOOP_READY;
  1199. }
  1200. /*
  1201. * PCI driver interface
  1202. */
  1203. static int __devinit
  1204. qla2x00_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
  1205. {
  1206. int ret = -ENODEV;
  1207. device_reg_t __iomem *reg;
  1208. struct Scsi_Host *host;
  1209. scsi_qla_host_t *ha;
  1210. unsigned long flags = 0;
  1211. char pci_info[20];
  1212. char fw_str[30];
  1213. struct scsi_host_template *sht;
  1214. if (pci_enable_device(pdev))
  1215. goto probe_out;
  1216. sht = &qla2x00_driver_template;
  1217. if (pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2422 ||
  1218. pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2432 ||
  1219. pdev->device == PCI_DEVICE_ID_QLOGIC_ISP5422 ||
  1220. pdev->device == PCI_DEVICE_ID_QLOGIC_ISP5432)
  1221. sht = &qla24xx_driver_template;
  1222. host = scsi_host_alloc(sht, sizeof(scsi_qla_host_t));
  1223. if (host == NULL) {
  1224. printk(KERN_WARNING
  1225. "qla2xxx: Couldn't allocate host from scsi layer!\n");
  1226. goto probe_disable_device;
  1227. }
  1228. /* Clear our data area */
  1229. ha = (scsi_qla_host_t *)host->hostdata;
  1230. memset(ha, 0, sizeof(scsi_qla_host_t));
  1231. ha->pdev = pdev;
  1232. ha->host = host;
  1233. ha->host_no = host->host_no;
  1234. sprintf(ha->host_str, "%s_%ld", QLA2XXX_DRIVER_NAME, ha->host_no);
  1235. ha->parent = NULL;
  1236. /* Set ISP-type information. */
  1237. qla2x00_set_isp_flags(ha);
  1238. /* Configure PCI I/O space */
  1239. ret = qla2x00_iospace_config(ha);
  1240. if (ret)
  1241. goto probe_failed;
  1242. qla_printk(KERN_INFO, ha,
  1243. "Found an ISP%04X, irq %d, iobase 0x%p\n", pdev->device, pdev->irq,
  1244. ha->iobase);
  1245. spin_lock_init(&ha->hardware_lock);
  1246. ha->prev_topology = 0;
  1247. ha->init_cb_size = sizeof(init_cb_t);
  1248. ha->mgmt_svr_loop_id = MANAGEMENT_SERVER + ha->vp_idx;
  1249. ha->link_data_rate = PORT_SPEED_UNKNOWN;
  1250. ha->optrom_size = OPTROM_SIZE_2300;
  1251. ha->max_q_depth = MAX_Q_DEPTH;
  1252. if (ql2xmaxqdepth != 0 && ql2xmaxqdepth <= 0xffffU)
  1253. ha->max_q_depth = ql2xmaxqdepth;
  1254. /* Assign ISP specific operations. */
  1255. ha->isp_ops.pci_config = qla2100_pci_config;
  1256. ha->isp_ops.reset_chip = qla2x00_reset_chip;
  1257. ha->isp_ops.chip_diag = qla2x00_chip_diag;
  1258. ha->isp_ops.config_rings = qla2x00_config_rings;
  1259. ha->isp_ops.reset_adapter = qla2x00_reset_adapter;
  1260. ha->isp_ops.nvram_config = qla2x00_nvram_config;
  1261. ha->isp_ops.update_fw_options = qla2x00_update_fw_options;
  1262. ha->isp_ops.load_risc = qla2x00_load_risc;
  1263. ha->isp_ops.pci_info_str = qla2x00_pci_info_str;
  1264. ha->isp_ops.fw_version_str = qla2x00_fw_version_str;
  1265. ha->isp_ops.intr_handler = qla2100_intr_handler;
  1266. ha->isp_ops.enable_intrs = qla2x00_enable_intrs;
  1267. ha->isp_ops.disable_intrs = qla2x00_disable_intrs;
  1268. ha->isp_ops.abort_command = qla2x00_abort_command;
  1269. ha->isp_ops.abort_target = qla2x00_abort_target;
  1270. ha->isp_ops.fabric_login = qla2x00_login_fabric;
  1271. ha->isp_ops.fabric_logout = qla2x00_fabric_logout;
  1272. ha->isp_ops.calc_req_entries = qla2x00_calc_iocbs_32;
  1273. ha->isp_ops.build_iocbs = qla2x00_build_scsi_iocbs_32;
  1274. ha->isp_ops.prep_ms_iocb = qla2x00_prep_ms_iocb;
  1275. ha->isp_ops.prep_ms_fdmi_iocb = qla2x00_prep_ms_fdmi_iocb;
  1276. ha->isp_ops.read_nvram = qla2x00_read_nvram_data;
  1277. ha->isp_ops.write_nvram = qla2x00_write_nvram_data;
  1278. ha->isp_ops.fw_dump = qla2100_fw_dump;
  1279. ha->isp_ops.read_optrom = qla2x00_read_optrom_data;
  1280. ha->isp_ops.write_optrom = qla2x00_write_optrom_data;
  1281. ha->isp_ops.get_flash_version = qla2x00_get_flash_version;
  1282. if (IS_QLA2100(ha)) {
  1283. host->max_id = MAX_TARGETS_2100;
  1284. ha->mbx_count = MAILBOX_REGISTER_COUNT_2100;
  1285. ha->request_q_length = REQUEST_ENTRY_CNT_2100;
  1286. ha->response_q_length = RESPONSE_ENTRY_CNT_2100;
  1287. ha->last_loop_id = SNS_LAST_LOOP_ID_2100;
  1288. host->sg_tablesize = 32;
  1289. ha->gid_list_info_size = 4;
  1290. } else if (IS_QLA2200(ha)) {
  1291. host->max_id = MAX_TARGETS_2200;
  1292. ha->mbx_count = MAILBOX_REGISTER_COUNT;
  1293. ha->request_q_length = REQUEST_ENTRY_CNT_2200;
  1294. ha->response_q_length = RESPONSE_ENTRY_CNT_2100;
  1295. ha->last_loop_id = SNS_LAST_LOOP_ID_2100;
  1296. ha->gid_list_info_size = 4;
  1297. } else if (IS_QLA23XX(ha)) {
  1298. host->max_id = MAX_TARGETS_2200;
  1299. ha->mbx_count = MAILBOX_REGISTER_COUNT;
  1300. ha->request_q_length = REQUEST_ENTRY_CNT_2200;
  1301. ha->response_q_length = RESPONSE_ENTRY_CNT_2300;
  1302. ha->last_loop_id = SNS_LAST_LOOP_ID_2300;
  1303. ha->isp_ops.pci_config = qla2300_pci_config;
  1304. ha->isp_ops.intr_handler = qla2300_intr_handler;
  1305. ha->isp_ops.fw_dump = qla2300_fw_dump;
  1306. ha->isp_ops.beacon_on = qla2x00_beacon_on;
  1307. ha->isp_ops.beacon_off = qla2x00_beacon_off;
  1308. ha->isp_ops.beacon_blink = qla2x00_beacon_blink;
  1309. ha->gid_list_info_size = 6;
  1310. if (IS_QLA2322(ha) || IS_QLA6322(ha))
  1311. ha->optrom_size = OPTROM_SIZE_2322;
  1312. } else if (IS_QLA24XX(ha) || IS_QLA54XX(ha)) {
  1313. host->max_id = MAX_TARGETS_2200;
  1314. ha->mbx_count = MAILBOX_REGISTER_COUNT;
  1315. ha->request_q_length = REQUEST_ENTRY_CNT_24XX;
  1316. ha->response_q_length = RESPONSE_ENTRY_CNT_2300;
  1317. ha->last_loop_id = SNS_LAST_LOOP_ID_2300;
  1318. ha->init_cb_size = sizeof(struct mid_init_cb_24xx);
  1319. ha->mgmt_svr_loop_id = 10 + ha->vp_idx;
  1320. ha->isp_ops.pci_config = qla24xx_pci_config;
  1321. ha->isp_ops.reset_chip = qla24xx_reset_chip;
  1322. ha->isp_ops.chip_diag = qla24xx_chip_diag;
  1323. ha->isp_ops.config_rings = qla24xx_config_rings;
  1324. ha->isp_ops.reset_adapter = qla24xx_reset_adapter;
  1325. ha->isp_ops.nvram_config = qla24xx_nvram_config;
  1326. ha->isp_ops.update_fw_options = qla24xx_update_fw_options;
  1327. ha->isp_ops.load_risc = qla24xx_load_risc;
  1328. ha->isp_ops.pci_info_str = qla24xx_pci_info_str;
  1329. ha->isp_ops.fw_version_str = qla24xx_fw_version_str;
  1330. ha->isp_ops.intr_handler = qla24xx_intr_handler;
  1331. ha->isp_ops.enable_intrs = qla24xx_enable_intrs;
  1332. ha->isp_ops.disable_intrs = qla24xx_disable_intrs;
  1333. ha->isp_ops.abort_command = qla24xx_abort_command;
  1334. ha->isp_ops.abort_target = qla24xx_abort_target;
  1335. ha->isp_ops.fabric_login = qla24xx_login_fabric;
  1336. ha->isp_ops.fabric_logout = qla24xx_fabric_logout;
  1337. ha->isp_ops.prep_ms_iocb = qla24xx_prep_ms_iocb;
  1338. ha->isp_ops.prep_ms_fdmi_iocb = qla24xx_prep_ms_fdmi_iocb;
  1339. ha->isp_ops.read_nvram = qla24xx_read_nvram_data;
  1340. ha->isp_ops.write_nvram = qla24xx_write_nvram_data;
  1341. ha->isp_ops.fw_dump = qla24xx_fw_dump;
  1342. ha->isp_ops.read_optrom = qla24xx_read_optrom_data;
  1343. ha->isp_ops.write_optrom = qla24xx_write_optrom_data;
  1344. ha->isp_ops.beacon_on = qla24xx_beacon_on;
  1345. ha->isp_ops.beacon_off = qla24xx_beacon_off;
  1346. ha->isp_ops.beacon_blink = qla24xx_beacon_blink;
  1347. ha->isp_ops.get_flash_version = qla24xx_get_flash_version;
  1348. ha->gid_list_info_size = 8;
  1349. ha->optrom_size = OPTROM_SIZE_24XX;
  1350. }
  1351. host->can_queue = ha->request_q_length + 128;
  1352. /* load the F/W, read paramaters, and init the H/W */
  1353. ha->instance = num_hosts;
  1354. init_MUTEX(&ha->mbx_cmd_sem);
  1355. init_MUTEX(&ha->vport_sem);
  1356. init_MUTEX_LOCKED(&ha->mbx_intr_sem);
  1357. INIT_LIST_HEAD(&ha->list);
  1358. INIT_LIST_HEAD(&ha->fcports);
  1359. INIT_LIST_HEAD(&ha->vp_list);
  1360. set_bit(0, (unsigned long *) ha->vp_idx_map);
  1361. qla2x00_config_dma_addressing(ha);
  1362. if (qla2x00_mem_alloc(ha)) {
  1363. qla_printk(KERN_WARNING, ha,
  1364. "[ERROR] Failed to allocate memory for adapter\n");
  1365. ret = -ENOMEM;
  1366. goto probe_failed;
  1367. }
  1368. if (qla2x00_initialize_adapter(ha)) {
  1369. qla_printk(KERN_WARNING, ha,
  1370. "Failed to initialize adapter\n");
  1371. DEBUG2(printk("scsi(%ld): Failed to initialize adapter - "
  1372. "Adapter flags %x.\n",
  1373. ha->host_no, ha->device_flags));
  1374. ret = -ENODEV;
  1375. goto probe_failed;
  1376. }
  1377. /*
  1378. * Startup the kernel thread for this host adapter
  1379. */
  1380. ha->dpc_thread = kthread_create(qla2x00_do_dpc, ha,
  1381. "%s_dpc", ha->host_str);
  1382. if (IS_ERR(ha->dpc_thread)) {
  1383. qla_printk(KERN_WARNING, ha,
  1384. "Unable to start DPC thread!\n");
  1385. ret = PTR_ERR(ha->dpc_thread);
  1386. goto probe_failed;
  1387. }
  1388. host->this_id = 255;
  1389. host->cmd_per_lun = 3;
  1390. host->unique_id = ha->instance;
  1391. host->max_cmd_len = MAX_CMDSZ;
  1392. host->max_channel = MAX_BUSES - 1;
  1393. host->max_lun = MAX_LUNS;
  1394. host->transportt = qla2xxx_transport_template;
  1395. ret = qla2x00_request_irqs(ha);
  1396. if (ret)
  1397. goto probe_failed;
  1398. /* Initialized the timer */
  1399. qla2x00_start_timer(ha, qla2x00_timer, WATCH_INTERVAL);
  1400. DEBUG2(printk("DEBUG: detect hba %ld at address = %p\n",
  1401. ha->host_no, ha));
  1402. ha->isp_ops.disable_intrs(ha);
  1403. spin_lock_irqsave(&ha->hardware_lock, flags);
  1404. reg = ha->iobase;
  1405. if (IS_FWI2_CAPABLE(ha)) {
  1406. WRT_REG_DWORD(&reg->isp24.hccr, HCCRX_CLR_HOST_INT);
  1407. WRT_REG_DWORD(&reg->isp24.hccr, HCCRX_CLR_RISC_INT);
  1408. } else {
  1409. WRT_REG_WORD(&reg->isp.semaphore, 0);
  1410. WRT_REG_WORD(&reg->isp.hccr, HCCR_CLR_RISC_INT);
  1411. WRT_REG_WORD(&reg->isp.hccr, HCCR_CLR_HOST_INT);
  1412. /* Enable proper parity */
  1413. if (!IS_QLA2100(ha) && !IS_QLA2200(ha)) {
  1414. if (IS_QLA2300(ha))
  1415. /* SRAM parity */
  1416. WRT_REG_WORD(&reg->isp.hccr,
  1417. (HCCR_ENABLE_PARITY + 0x1));
  1418. else
  1419. /* SRAM, Instruction RAM and GP RAM parity */
  1420. WRT_REG_WORD(&reg->isp.hccr,
  1421. (HCCR_ENABLE_PARITY + 0x7));
  1422. }
  1423. }
  1424. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1425. ha->isp_ops.enable_intrs(ha);
  1426. pci_set_drvdata(pdev, ha);
  1427. ha->flags.init_done = 1;
  1428. ha->flags.online = 1;
  1429. num_hosts++;
  1430. ret = scsi_add_host(host, &pdev->dev);
  1431. if (ret)
  1432. goto probe_failed;
  1433. scsi_scan_host(host);
  1434. qla2x00_alloc_sysfs_attr(ha);
  1435. qla2x00_init_host_attr(ha);
  1436. qla_printk(KERN_INFO, ha, "\n"
  1437. " QLogic Fibre Channel HBA Driver: %s\n"
  1438. " QLogic %s - %s\n"
  1439. " ISP%04X: %s @ %s hdma%c, host#=%ld, fw=%s\n",
  1440. qla2x00_version_str, ha->model_number,
  1441. ha->model_desc ? ha->model_desc: "", pdev->device,
  1442. ha->isp_ops.pci_info_str(ha, pci_info), pci_name(pdev),
  1443. ha->flags.enable_64bit_addressing ? '+': '-', ha->host_no,
  1444. ha->isp_ops.fw_version_str(ha, fw_str));
  1445. return 0;
  1446. probe_failed:
  1447. qla2x00_free_device(ha);
  1448. scsi_host_put(host);
  1449. probe_disable_device:
  1450. pci_disable_device(pdev);
  1451. probe_out:
  1452. return ret;
  1453. }
  1454. static void __devexit
  1455. qla2x00_remove_one(struct pci_dev *pdev)
  1456. {
  1457. scsi_qla_host_t *ha;
  1458. ha = pci_get_drvdata(pdev);
  1459. qla2x00_free_sysfs_attr(ha);
  1460. fc_remove_host(ha->host);
  1461. scsi_remove_host(ha->host);
  1462. qla2x00_free_device(ha);
  1463. scsi_host_put(ha->host);
  1464. pci_disable_device(pdev);
  1465. pci_set_drvdata(pdev, NULL);
  1466. }
  1467. static void
  1468. qla2x00_free_device(scsi_qla_host_t *ha)
  1469. {
  1470. /* Disable timer */
  1471. if (ha->timer_active)
  1472. qla2x00_stop_timer(ha);
  1473. /* Kill the kernel thread for this host */
  1474. if (ha->dpc_thread) {
  1475. struct task_struct *t = ha->dpc_thread;
  1476. /*
  1477. * qla2xxx_wake_dpc checks for ->dpc_thread
  1478. * so we need to zero it out.
  1479. */
  1480. ha->dpc_thread = NULL;
  1481. kthread_stop(t);
  1482. }
  1483. if (ha->eft)
  1484. qla2x00_trace_control(ha, TC_DISABLE, 0, 0);
  1485. ha->flags.online = 0;
  1486. /* Stop currently executing firmware. */
  1487. qla2x00_try_to_stop_firmware(ha);
  1488. /* turn-off interrupts on the card */
  1489. if (ha->interrupts_on)
  1490. ha->isp_ops.disable_intrs(ha);
  1491. qla2x00_mem_free(ha);
  1492. qla2x00_free_irqs(ha);
  1493. /* release io space registers */
  1494. if (ha->iobase)
  1495. iounmap(ha->iobase);
  1496. pci_release_regions(ha->pdev);
  1497. }
  1498. static inline void
  1499. qla2x00_schedule_rport_del(struct scsi_qla_host *ha, fc_port_t *fcport,
  1500. int defer)
  1501. {
  1502. unsigned long flags;
  1503. struct fc_rport *rport;
  1504. if (!fcport->rport)
  1505. return;
  1506. rport = fcport->rport;
  1507. if (defer) {
  1508. spin_lock_irqsave(&fcport->rport_lock, flags);
  1509. fcport->drport = rport;
  1510. fcport->rport = NULL;
  1511. *(fc_port_t **)rport->dd_data = NULL;
  1512. spin_unlock_irqrestore(&fcport->rport_lock, flags);
  1513. set_bit(FCPORT_UPDATE_NEEDED, &ha->dpc_flags);
  1514. } else {
  1515. spin_lock_irqsave(&fcport->rport_lock, flags);
  1516. fcport->rport = NULL;
  1517. *(fc_port_t **)rport->dd_data = NULL;
  1518. spin_unlock_irqrestore(&fcport->rport_lock, flags);
  1519. fc_remote_port_delete(rport);
  1520. }
  1521. }
  1522. /*
  1523. * qla2x00_mark_device_lost Updates fcport state when device goes offline.
  1524. *
  1525. * Input: ha = adapter block pointer. fcport = port structure pointer.
  1526. *
  1527. * Return: None.
  1528. *
  1529. * Context:
  1530. */
  1531. void qla2x00_mark_device_lost(scsi_qla_host_t *ha, fc_port_t *fcport,
  1532. int do_login, int defer)
  1533. {
  1534. if (atomic_read(&fcport->state) == FCS_ONLINE &&
  1535. ha->vp_idx == fcport->vp_idx)
  1536. qla2x00_schedule_rport_del(ha, fcport, defer);
  1537. /*
  1538. * We may need to retry the login, so don't change the state of the
  1539. * port but do the retries.
  1540. */
  1541. if (atomic_read(&fcport->state) != FCS_DEVICE_DEAD)
  1542. atomic_set(&fcport->state, FCS_DEVICE_LOST);
  1543. if (!do_login)
  1544. return;
  1545. if (fcport->login_retry == 0) {
  1546. fcport->login_retry = ha->login_retry_count;
  1547. set_bit(RELOGIN_NEEDED, &ha->dpc_flags);
  1548. DEBUG(printk("scsi(%ld): Port login retry: "
  1549. "%02x%02x%02x%02x%02x%02x%02x%02x, "
  1550. "id = 0x%04x retry cnt=%d\n",
  1551. ha->host_no,
  1552. fcport->port_name[0],
  1553. fcport->port_name[1],
  1554. fcport->port_name[2],
  1555. fcport->port_name[3],
  1556. fcport->port_name[4],
  1557. fcport->port_name[5],
  1558. fcport->port_name[6],
  1559. fcport->port_name[7],
  1560. fcport->loop_id,
  1561. fcport->login_retry));
  1562. }
  1563. }
  1564. /*
  1565. * qla2x00_mark_all_devices_lost
  1566. * Updates fcport state when device goes offline.
  1567. *
  1568. * Input:
  1569. * ha = adapter block pointer.
  1570. * fcport = port structure pointer.
  1571. *
  1572. * Return:
  1573. * None.
  1574. *
  1575. * Context:
  1576. */
  1577. void
  1578. qla2x00_mark_all_devices_lost(scsi_qla_host_t *ha, int defer)
  1579. {
  1580. fc_port_t *fcport;
  1581. scsi_qla_host_t *pha = to_qla_parent(ha);
  1582. list_for_each_entry(fcport, &pha->fcports, list) {
  1583. if (ha->vp_idx != 0 && ha->vp_idx != fcport->vp_idx)
  1584. continue;
  1585. /*
  1586. * No point in marking the device as lost, if the device is
  1587. * already DEAD.
  1588. */
  1589. if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD)
  1590. continue;
  1591. if (atomic_read(&fcport->state) == FCS_ONLINE) {
  1592. if (defer)
  1593. qla2x00_schedule_rport_del(ha, fcport, defer);
  1594. else if (ha->vp_idx == fcport->vp_idx)
  1595. qla2x00_schedule_rport_del(ha, fcport, defer);
  1596. }
  1597. atomic_set(&fcport->state, FCS_DEVICE_LOST);
  1598. }
  1599. if (defer)
  1600. qla2xxx_wake_dpc(ha);
  1601. }
  1602. /*
  1603. * qla2x00_mem_alloc
  1604. * Allocates adapter memory.
  1605. *
  1606. * Returns:
  1607. * 0 = success.
  1608. * 1 = failure.
  1609. */
  1610. uint8_t
  1611. qla2x00_mem_alloc(scsi_qla_host_t *ha)
  1612. {
  1613. char name[16];
  1614. uint8_t status = 1;
  1615. int retry= 10;
  1616. do {
  1617. /*
  1618. * This will loop only once if everything goes well, else some
  1619. * number of retries will be performed to get around a kernel
  1620. * bug where available mem is not allocated until after a
  1621. * little delay and a retry.
  1622. */
  1623. ha->request_ring = dma_alloc_coherent(&ha->pdev->dev,
  1624. (ha->request_q_length + 1) * sizeof(request_t),
  1625. &ha->request_dma, GFP_KERNEL);
  1626. if (ha->request_ring == NULL) {
  1627. qla_printk(KERN_WARNING, ha,
  1628. "Memory Allocation failed - request_ring\n");
  1629. qla2x00_mem_free(ha);
  1630. msleep(100);
  1631. continue;
  1632. }
  1633. ha->response_ring = dma_alloc_coherent(&ha->pdev->dev,
  1634. (ha->response_q_length + 1) * sizeof(response_t),
  1635. &ha->response_dma, GFP_KERNEL);
  1636. if (ha->response_ring == NULL) {
  1637. qla_printk(KERN_WARNING, ha,
  1638. "Memory Allocation failed - response_ring\n");
  1639. qla2x00_mem_free(ha);
  1640. msleep(100);
  1641. continue;
  1642. }
  1643. ha->gid_list = dma_alloc_coherent(&ha->pdev->dev, GID_LIST_SIZE,
  1644. &ha->gid_list_dma, GFP_KERNEL);
  1645. if (ha->gid_list == NULL) {
  1646. qla_printk(KERN_WARNING, ha,
  1647. "Memory Allocation failed - gid_list\n");
  1648. qla2x00_mem_free(ha);
  1649. msleep(100);
  1650. continue;
  1651. }
  1652. /* get consistent memory allocated for init control block */
  1653. ha->init_cb = dma_alloc_coherent(&ha->pdev->dev,
  1654. ha->init_cb_size, &ha->init_cb_dma, GFP_KERNEL);
  1655. if (ha->init_cb == NULL) {
  1656. qla_printk(KERN_WARNING, ha,
  1657. "Memory Allocation failed - init_cb\n");
  1658. qla2x00_mem_free(ha);
  1659. msleep(100);
  1660. continue;
  1661. }
  1662. memset(ha->init_cb, 0, ha->init_cb_size);
  1663. snprintf(name, sizeof(name), "%s_%ld", QLA2XXX_DRIVER_NAME,
  1664. ha->host_no);
  1665. ha->s_dma_pool = dma_pool_create(name, &ha->pdev->dev,
  1666. DMA_POOL_SIZE, 8, 0);
  1667. if (ha->s_dma_pool == NULL) {
  1668. qla_printk(KERN_WARNING, ha,
  1669. "Memory Allocation failed - s_dma_pool\n");
  1670. qla2x00_mem_free(ha);
  1671. msleep(100);
  1672. continue;
  1673. }
  1674. if (qla2x00_allocate_sp_pool(ha)) {
  1675. qla_printk(KERN_WARNING, ha,
  1676. "Memory Allocation failed - "
  1677. "qla2x00_allocate_sp_pool()\n");
  1678. qla2x00_mem_free(ha);
  1679. msleep(100);
  1680. continue;
  1681. }
  1682. /* Allocate memory for SNS commands */
  1683. if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
  1684. /* Get consistent memory allocated for SNS commands */
  1685. ha->sns_cmd = dma_alloc_coherent(&ha->pdev->dev,
  1686. sizeof(struct sns_cmd_pkt), &ha->sns_cmd_dma,
  1687. GFP_KERNEL);
  1688. if (ha->sns_cmd == NULL) {
  1689. /* error */
  1690. qla_printk(KERN_WARNING, ha,
  1691. "Memory Allocation failed - sns_cmd\n");
  1692. qla2x00_mem_free(ha);
  1693. msleep(100);
  1694. continue;
  1695. }
  1696. memset(ha->sns_cmd, 0, sizeof(struct sns_cmd_pkt));
  1697. } else {
  1698. /* Get consistent memory allocated for MS IOCB */
  1699. ha->ms_iocb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
  1700. &ha->ms_iocb_dma);
  1701. if (ha->ms_iocb == NULL) {
  1702. /* error */
  1703. qla_printk(KERN_WARNING, ha,
  1704. "Memory Allocation failed - ms_iocb\n");
  1705. qla2x00_mem_free(ha);
  1706. msleep(100);
  1707. continue;
  1708. }
  1709. memset(ha->ms_iocb, 0, sizeof(ms_iocb_entry_t));
  1710. /*
  1711. * Get consistent memory allocated for CT SNS
  1712. * commands
  1713. */
  1714. ha->ct_sns = dma_alloc_coherent(&ha->pdev->dev,
  1715. sizeof(struct ct_sns_pkt), &ha->ct_sns_dma,
  1716. GFP_KERNEL);
  1717. if (ha->ct_sns == NULL) {
  1718. /* error */
  1719. qla_printk(KERN_WARNING, ha,
  1720. "Memory Allocation failed - ct_sns\n");
  1721. qla2x00_mem_free(ha);
  1722. msleep(100);
  1723. continue;
  1724. }
  1725. memset(ha->ct_sns, 0, sizeof(struct ct_sns_pkt));
  1726. if (IS_FWI2_CAPABLE(ha)) {
  1727. /*
  1728. * Get consistent memory allocated for SFP
  1729. * block.
  1730. */
  1731. ha->sfp_data = dma_pool_alloc(ha->s_dma_pool,
  1732. GFP_KERNEL, &ha->sfp_data_dma);
  1733. if (ha->sfp_data == NULL) {
  1734. qla_printk(KERN_WARNING, ha,
  1735. "Memory Allocation failed - "
  1736. "sfp_data\n");
  1737. qla2x00_mem_free(ha);
  1738. msleep(100);
  1739. continue;
  1740. }
  1741. memset(ha->sfp_data, 0, SFP_BLOCK_SIZE);
  1742. }
  1743. }
  1744. /* Done all allocations without any error. */
  1745. status = 0;
  1746. } while (retry-- && status != 0);
  1747. if (status) {
  1748. printk(KERN_WARNING
  1749. "%s(): **** FAILED ****\n", __func__);
  1750. }
  1751. return(status);
  1752. }
  1753. /*
  1754. * qla2x00_mem_free
  1755. * Frees all adapter allocated memory.
  1756. *
  1757. * Input:
  1758. * ha = adapter block pointer.
  1759. */
  1760. void
  1761. qla2x00_mem_free(scsi_qla_host_t *ha)
  1762. {
  1763. struct list_head *fcpl, *fcptemp;
  1764. fc_port_t *fcport;
  1765. if (ha == NULL) {
  1766. /* error */
  1767. DEBUG2(printk("%s(): ERROR invalid ha pointer.\n", __func__));
  1768. return;
  1769. }
  1770. /* free sp pool */
  1771. qla2x00_free_sp_pool(ha);
  1772. if (ha->fw_dump) {
  1773. if (ha->eft)
  1774. dma_free_coherent(&ha->pdev->dev,
  1775. ntohl(ha->fw_dump->eft_size), ha->eft, ha->eft_dma);
  1776. vfree(ha->fw_dump);
  1777. }
  1778. if (ha->sns_cmd)
  1779. dma_free_coherent(&ha->pdev->dev, sizeof(struct sns_cmd_pkt),
  1780. ha->sns_cmd, ha->sns_cmd_dma);
  1781. if (ha->ct_sns)
  1782. dma_free_coherent(&ha->pdev->dev, sizeof(struct ct_sns_pkt),
  1783. ha->ct_sns, ha->ct_sns_dma);
  1784. if (ha->sfp_data)
  1785. dma_pool_free(ha->s_dma_pool, ha->sfp_data, ha->sfp_data_dma);
  1786. if (ha->ms_iocb)
  1787. dma_pool_free(ha->s_dma_pool, ha->ms_iocb, ha->ms_iocb_dma);
  1788. if (ha->s_dma_pool)
  1789. dma_pool_destroy(ha->s_dma_pool);
  1790. if (ha->init_cb)
  1791. dma_free_coherent(&ha->pdev->dev, ha->init_cb_size,
  1792. ha->init_cb, ha->init_cb_dma);
  1793. if (ha->gid_list)
  1794. dma_free_coherent(&ha->pdev->dev, GID_LIST_SIZE, ha->gid_list,
  1795. ha->gid_list_dma);
  1796. if (ha->response_ring)
  1797. dma_free_coherent(&ha->pdev->dev,
  1798. (ha->response_q_length + 1) * sizeof(response_t),
  1799. ha->response_ring, ha->response_dma);
  1800. if (ha->request_ring)
  1801. dma_free_coherent(&ha->pdev->dev,
  1802. (ha->request_q_length + 1) * sizeof(request_t),
  1803. ha->request_ring, ha->request_dma);
  1804. ha->eft = NULL;
  1805. ha->eft_dma = 0;
  1806. ha->sns_cmd = NULL;
  1807. ha->sns_cmd_dma = 0;
  1808. ha->ct_sns = NULL;
  1809. ha->ct_sns_dma = 0;
  1810. ha->ms_iocb = NULL;
  1811. ha->ms_iocb_dma = 0;
  1812. ha->init_cb = NULL;
  1813. ha->init_cb_dma = 0;
  1814. ha->s_dma_pool = NULL;
  1815. ha->gid_list = NULL;
  1816. ha->gid_list_dma = 0;
  1817. ha->response_ring = NULL;
  1818. ha->response_dma = 0;
  1819. ha->request_ring = NULL;
  1820. ha->request_dma = 0;
  1821. list_for_each_safe(fcpl, fcptemp, &ha->fcports) {
  1822. fcport = list_entry(fcpl, fc_port_t, list);
  1823. /* fc ports */
  1824. list_del_init(&fcport->list);
  1825. kfree(fcport);
  1826. }
  1827. INIT_LIST_HEAD(&ha->fcports);
  1828. ha->fw_dump = NULL;
  1829. ha->fw_dumped = 0;
  1830. ha->fw_dump_reading = 0;
  1831. vfree(ha->optrom_buffer);
  1832. }
  1833. /*
  1834. * qla2x00_allocate_sp_pool
  1835. * This routine is called during initialization to allocate
  1836. * memory for local srb_t.
  1837. *
  1838. * Input:
  1839. * ha = adapter block pointer.
  1840. *
  1841. * Context:
  1842. * Kernel context.
  1843. */
  1844. static int
  1845. qla2x00_allocate_sp_pool(scsi_qla_host_t *ha)
  1846. {
  1847. int rval;
  1848. rval = QLA_SUCCESS;
  1849. ha->srb_mempool = mempool_create_slab_pool(SRB_MIN_REQ, srb_cachep);
  1850. if (ha->srb_mempool == NULL) {
  1851. qla_printk(KERN_INFO, ha, "Unable to allocate SRB mempool.\n");
  1852. rval = QLA_FUNCTION_FAILED;
  1853. }
  1854. return (rval);
  1855. }
  1856. /*
  1857. * This routine frees all adapter allocated memory.
  1858. *
  1859. */
  1860. static void
  1861. qla2x00_free_sp_pool( scsi_qla_host_t *ha)
  1862. {
  1863. if (ha->srb_mempool) {
  1864. mempool_destroy(ha->srb_mempool);
  1865. ha->srb_mempool = NULL;
  1866. }
  1867. }
  1868. /**************************************************************************
  1869. * qla2x00_do_dpc
  1870. * This kernel thread is a task that is schedule by the interrupt handler
  1871. * to perform the background processing for interrupts.
  1872. *
  1873. * Notes:
  1874. * This task always run in the context of a kernel thread. It
  1875. * is kick-off by the driver's detect code and starts up
  1876. * up one per adapter. It immediately goes to sleep and waits for
  1877. * some fibre event. When either the interrupt handler or
  1878. * the timer routine detects a event it will one of the task
  1879. * bits then wake us up.
  1880. **************************************************************************/
  1881. static int
  1882. qla2x00_do_dpc(void *data)
  1883. {
  1884. int rval;
  1885. scsi_qla_host_t *ha;
  1886. fc_port_t *fcport;
  1887. uint8_t status;
  1888. uint16_t next_loopid;
  1889. ha = (scsi_qla_host_t *)data;
  1890. set_user_nice(current, -20);
  1891. while (!kthread_should_stop()) {
  1892. DEBUG3(printk("qla2x00: DPC handler sleeping\n"));
  1893. set_current_state(TASK_INTERRUPTIBLE);
  1894. schedule();
  1895. __set_current_state(TASK_RUNNING);
  1896. DEBUG3(printk("qla2x00: DPC handler waking up\n"));
  1897. /* Initialization not yet finished. Don't do anything yet. */
  1898. if (!ha->flags.init_done)
  1899. continue;
  1900. DEBUG3(printk("scsi(%ld): DPC handler\n", ha->host_no));
  1901. ha->dpc_active = 1;
  1902. if (ha->flags.mbox_busy) {
  1903. ha->dpc_active = 0;
  1904. continue;
  1905. }
  1906. if (test_and_clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
  1907. DEBUG(printk("scsi(%ld): dpc: sched "
  1908. "qla2x00_abort_isp ha = %p\n",
  1909. ha->host_no, ha));
  1910. if (!(test_and_set_bit(ABORT_ISP_ACTIVE,
  1911. &ha->dpc_flags))) {
  1912. if (qla2x00_abort_isp(ha)) {
  1913. /* failed. retry later */
  1914. set_bit(ISP_ABORT_NEEDED,
  1915. &ha->dpc_flags);
  1916. }
  1917. clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
  1918. }
  1919. DEBUG(printk("scsi(%ld): dpc: qla2x00_abort_isp end\n",
  1920. ha->host_no));
  1921. }
  1922. if (test_and_clear_bit(FCPORT_UPDATE_NEEDED, &ha->dpc_flags))
  1923. qla2x00_update_fcports(ha);
  1924. if (test_and_clear_bit(LOOP_RESET_NEEDED, &ha->dpc_flags)) {
  1925. DEBUG(printk("scsi(%ld): dpc: sched loop_reset()\n",
  1926. ha->host_no));
  1927. qla2x00_loop_reset(ha);
  1928. }
  1929. if (test_and_clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags) &&
  1930. (!(test_and_set_bit(RESET_ACTIVE, &ha->dpc_flags)))) {
  1931. DEBUG(printk("scsi(%ld): qla2x00_reset_marker()\n",
  1932. ha->host_no));
  1933. qla2x00_rst_aen(ha);
  1934. clear_bit(RESET_ACTIVE, &ha->dpc_flags);
  1935. }
  1936. /* Retry each device up to login retry count */
  1937. if ((test_and_clear_bit(RELOGIN_NEEDED, &ha->dpc_flags)) &&
  1938. !test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags) &&
  1939. atomic_read(&ha->loop_state) != LOOP_DOWN) {
  1940. DEBUG(printk("scsi(%ld): qla2x00_port_login()\n",
  1941. ha->host_no));
  1942. next_loopid = 0;
  1943. list_for_each_entry(fcport, &ha->fcports, list) {
  1944. /*
  1945. * If the port is not ONLINE then try to login
  1946. * to it if we haven't run out of retries.
  1947. */
  1948. if (atomic_read(&fcport->state) != FCS_ONLINE &&
  1949. fcport->login_retry) {
  1950. fcport->login_retry--;
  1951. if (fcport->flags & FCF_FABRIC_DEVICE) {
  1952. if (fcport->flags &
  1953. FCF_TAPE_PRESENT)
  1954. ha->isp_ops.fabric_logout(
  1955. ha, fcport->loop_id,
  1956. fcport->d_id.b.domain,
  1957. fcport->d_id.b.area,
  1958. fcport->d_id.b.al_pa);
  1959. status = qla2x00_fabric_login(
  1960. ha, fcport, &next_loopid);
  1961. } else
  1962. status =
  1963. qla2x00_local_device_login(
  1964. ha, fcport);
  1965. if (status == QLA_SUCCESS) {
  1966. fcport->old_loop_id = fcport->loop_id;
  1967. DEBUG(printk("scsi(%ld): port login OK: logged in ID 0x%x\n",
  1968. ha->host_no, fcport->loop_id));
  1969. qla2x00_update_fcport(ha,
  1970. fcport);
  1971. } else if (status == 1) {
  1972. set_bit(RELOGIN_NEEDED, &ha->dpc_flags);
  1973. /* retry the login again */
  1974. DEBUG(printk("scsi(%ld): Retrying %d login again loop_id 0x%x\n",
  1975. ha->host_no,
  1976. fcport->login_retry, fcport->loop_id));
  1977. } else {
  1978. fcport->login_retry = 0;
  1979. }
  1980. }
  1981. if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
  1982. break;
  1983. }
  1984. DEBUG(printk("scsi(%ld): qla2x00_port_login - end\n",
  1985. ha->host_no));
  1986. }
  1987. if ((test_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags)) &&
  1988. atomic_read(&ha->loop_state) != LOOP_DOWN) {
  1989. clear_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags);
  1990. DEBUG(printk("scsi(%ld): qla2x00_login_retry()\n",
  1991. ha->host_no));
  1992. set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
  1993. DEBUG(printk("scsi(%ld): qla2x00_login_retry - end\n",
  1994. ha->host_no));
  1995. }
  1996. if (test_and_clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
  1997. DEBUG(printk("scsi(%ld): qla2x00_loop_resync()\n",
  1998. ha->host_no));
  1999. if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE,
  2000. &ha->dpc_flags))) {
  2001. rval = qla2x00_loop_resync(ha);
  2002. clear_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags);
  2003. }
  2004. DEBUG(printk("scsi(%ld): qla2x00_loop_resync - end\n",
  2005. ha->host_no));
  2006. }
  2007. if (test_and_clear_bit(FCPORT_RESCAN_NEEDED, &ha->dpc_flags)) {
  2008. DEBUG(printk("scsi(%ld): Rescan flagged fcports...\n",
  2009. ha->host_no));
  2010. qla2x00_rescan_fcports(ha);
  2011. DEBUG(printk("scsi(%ld): Rescan flagged fcports..."
  2012. "end.\n",
  2013. ha->host_no));
  2014. }
  2015. if (!ha->interrupts_on)
  2016. ha->isp_ops.enable_intrs(ha);
  2017. if (test_and_clear_bit(BEACON_BLINK_NEEDED, &ha->dpc_flags))
  2018. ha->isp_ops.beacon_blink(ha);
  2019. qla2x00_do_dpc_all_vps(ha);
  2020. ha->dpc_active = 0;
  2021. } /* End of while(1) */
  2022. DEBUG(printk("scsi(%ld): DPC handler exiting\n", ha->host_no));
  2023. /*
  2024. * Make sure that nobody tries to wake us up again.
  2025. */
  2026. ha->dpc_active = 0;
  2027. return 0;
  2028. }
  2029. void
  2030. qla2xxx_wake_dpc(scsi_qla_host_t *ha)
  2031. {
  2032. if (ha->dpc_thread)
  2033. wake_up_process(ha->dpc_thread);
  2034. }
  2035. /*
  2036. * qla2x00_rst_aen
  2037. * Processes asynchronous reset.
  2038. *
  2039. * Input:
  2040. * ha = adapter block pointer.
  2041. */
  2042. static void
  2043. qla2x00_rst_aen(scsi_qla_host_t *ha)
  2044. {
  2045. if (ha->flags.online && !ha->flags.reset_active &&
  2046. !atomic_read(&ha->loop_down_timer) &&
  2047. !(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags))) {
  2048. do {
  2049. clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
  2050. /*
  2051. * Issue marker command only when we are going to start
  2052. * the I/O.
  2053. */
  2054. ha->marker_needed = 1;
  2055. } while (!atomic_read(&ha->loop_down_timer) &&
  2056. (test_bit(RESET_MARKER_NEEDED, &ha->dpc_flags)));
  2057. }
  2058. }
  2059. static void
  2060. qla2x00_sp_free_dma(scsi_qla_host_t *ha, srb_t *sp)
  2061. {
  2062. struct scsi_cmnd *cmd = sp->cmd;
  2063. if (sp->flags & SRB_DMA_VALID) {
  2064. scsi_dma_unmap(cmd);
  2065. sp->flags &= ~SRB_DMA_VALID;
  2066. }
  2067. CMD_SP(cmd) = NULL;
  2068. }
  2069. void
  2070. qla2x00_sp_compl(scsi_qla_host_t *ha, srb_t *sp)
  2071. {
  2072. struct scsi_cmnd *cmd = sp->cmd;
  2073. qla2x00_sp_free_dma(ha, sp);
  2074. mempool_free(sp, ha->srb_mempool);
  2075. cmd->scsi_done(cmd);
  2076. }
  2077. /**************************************************************************
  2078. * qla2x00_timer
  2079. *
  2080. * Description:
  2081. * One second timer
  2082. *
  2083. * Context: Interrupt
  2084. ***************************************************************************/
  2085. void
  2086. qla2x00_timer(scsi_qla_host_t *ha)
  2087. {
  2088. unsigned long cpu_flags = 0;
  2089. fc_port_t *fcport;
  2090. int start_dpc = 0;
  2091. int index;
  2092. srb_t *sp;
  2093. int t;
  2094. scsi_qla_host_t *pha = to_qla_parent(ha);
  2095. /*
  2096. * Ports - Port down timer.
  2097. *
  2098. * Whenever, a port is in the LOST state we start decrementing its port
  2099. * down timer every second until it reaches zero. Once it reaches zero
  2100. * the port it marked DEAD.
  2101. */
  2102. t = 0;
  2103. list_for_each_entry(fcport, &ha->fcports, list) {
  2104. if (fcport->port_type != FCT_TARGET)
  2105. continue;
  2106. if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
  2107. if (atomic_read(&fcport->port_down_timer) == 0)
  2108. continue;
  2109. if (atomic_dec_and_test(&fcport->port_down_timer) != 0)
  2110. atomic_set(&fcport->state, FCS_DEVICE_DEAD);
  2111. DEBUG(printk("scsi(%ld): fcport-%d - port retry count: "
  2112. "%d remaining\n",
  2113. ha->host_no,
  2114. t, atomic_read(&fcport->port_down_timer)));
  2115. }
  2116. t++;
  2117. } /* End of for fcport */
  2118. /* Loop down handler. */
  2119. if (atomic_read(&ha->loop_down_timer) > 0 &&
  2120. !(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags)) && ha->flags.online) {
  2121. if (atomic_read(&ha->loop_down_timer) ==
  2122. ha->loop_down_abort_time) {
  2123. DEBUG(printk("scsi(%ld): Loop Down - aborting the "
  2124. "queues before time expire\n",
  2125. ha->host_no));
  2126. if (!IS_QLA2100(ha) && ha->link_down_timeout)
  2127. atomic_set(&ha->loop_state, LOOP_DEAD);
  2128. /* Schedule an ISP abort to return any tape commands. */
  2129. /* NPIV - scan physical port only */
  2130. if (!ha->parent) {
  2131. spin_lock_irqsave(&ha->hardware_lock,
  2132. cpu_flags);
  2133. for (index = 1;
  2134. index < MAX_OUTSTANDING_COMMANDS;
  2135. index++) {
  2136. fc_port_t *sfcp;
  2137. sp = ha->outstanding_cmds[index];
  2138. if (!sp)
  2139. continue;
  2140. sfcp = sp->fcport;
  2141. if (!(sfcp->flags & FCF_TAPE_PRESENT))
  2142. continue;
  2143. set_bit(ISP_ABORT_NEEDED,
  2144. &ha->dpc_flags);
  2145. break;
  2146. }
  2147. spin_unlock_irqrestore(&ha->hardware_lock,
  2148. cpu_flags);
  2149. }
  2150. set_bit(ABORT_QUEUES_NEEDED, &ha->dpc_flags);
  2151. start_dpc++;
  2152. }
  2153. /* if the loop has been down for 4 minutes, reinit adapter */
  2154. if (atomic_dec_and_test(&ha->loop_down_timer) != 0) {
  2155. DEBUG(printk("scsi(%ld): Loop down exceed 4 mins - "
  2156. "restarting queues.\n",
  2157. ha->host_no));
  2158. set_bit(RESTART_QUEUES_NEEDED, &ha->dpc_flags);
  2159. start_dpc++;
  2160. if (!(ha->device_flags & DFLG_NO_CABLE)) {
  2161. DEBUG(printk("scsi(%ld): Loop down - "
  2162. "aborting ISP.\n",
  2163. ha->host_no));
  2164. qla_printk(KERN_WARNING, ha,
  2165. "Loop down - aborting ISP.\n");
  2166. set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  2167. }
  2168. }
  2169. DEBUG3(printk("scsi(%ld): Loop Down - seconds remaining %d\n",
  2170. ha->host_no,
  2171. atomic_read(&ha->loop_down_timer)));
  2172. }
  2173. /* Check if beacon LED needs to be blinked */
  2174. if (ha->beacon_blink_led == 1) {
  2175. set_bit(BEACON_BLINK_NEEDED, &ha->dpc_flags);
  2176. start_dpc++;
  2177. }
  2178. /* Schedule the DPC routine if needed */
  2179. if ((test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags) ||
  2180. test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags) ||
  2181. test_bit(LOOP_RESET_NEEDED, &ha->dpc_flags) ||
  2182. test_bit(FCPORT_UPDATE_NEEDED, &ha->dpc_flags) ||
  2183. start_dpc ||
  2184. test_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags) ||
  2185. test_bit(RESET_MARKER_NEEDED, &ha->dpc_flags) ||
  2186. test_bit(BEACON_BLINK_NEEDED, &ha->dpc_flags) ||
  2187. test_bit(VP_DPC_NEEDED, &ha->dpc_flags) ||
  2188. test_bit(RELOGIN_NEEDED, &ha->dpc_flags)))
  2189. qla2xxx_wake_dpc(pha);
  2190. qla2x00_restart_timer(ha, WATCH_INTERVAL);
  2191. }
  2192. /* XXX(hch): crude hack to emulate a down_timeout() */
  2193. int
  2194. qla2x00_down_timeout(struct semaphore *sema, unsigned long timeout)
  2195. {
  2196. const unsigned int step = 100; /* msecs */
  2197. unsigned int iterations = jiffies_to_msecs(timeout)/100;
  2198. do {
  2199. if (!down_trylock(sema))
  2200. return 0;
  2201. if (msleep_interruptible(step))
  2202. break;
  2203. } while (--iterations > 0);
  2204. return -ETIMEDOUT;
  2205. }
  2206. /* Firmware interface routines. */
  2207. #define FW_BLOBS 5
  2208. #define FW_ISP21XX 0
  2209. #define FW_ISP22XX 1
  2210. #define FW_ISP2300 2
  2211. #define FW_ISP2322 3
  2212. #define FW_ISP24XX 4
  2213. #define FW_FILE_ISP21XX "ql2100_fw.bin"
  2214. #define FW_FILE_ISP22XX "ql2200_fw.bin"
  2215. #define FW_FILE_ISP2300 "ql2300_fw.bin"
  2216. #define FW_FILE_ISP2322 "ql2322_fw.bin"
  2217. #define FW_FILE_ISP24XX "ql2400_fw.bin"
  2218. static DECLARE_MUTEX(qla_fw_lock);
  2219. static struct fw_blob qla_fw_blobs[FW_BLOBS] = {
  2220. { .name = FW_FILE_ISP21XX, .segs = { 0x1000, 0 }, },
  2221. { .name = FW_FILE_ISP22XX, .segs = { 0x1000, 0 }, },
  2222. { .name = FW_FILE_ISP2300, .segs = { 0x800, 0 }, },
  2223. { .name = FW_FILE_ISP2322, .segs = { 0x800, 0x1c000, 0x1e000, 0 }, },
  2224. { .name = FW_FILE_ISP24XX, },
  2225. };
  2226. struct fw_blob *
  2227. qla2x00_request_firmware(scsi_qla_host_t *ha)
  2228. {
  2229. struct fw_blob *blob;
  2230. blob = NULL;
  2231. if (IS_QLA2100(ha)) {
  2232. blob = &qla_fw_blobs[FW_ISP21XX];
  2233. } else if (IS_QLA2200(ha)) {
  2234. blob = &qla_fw_blobs[FW_ISP22XX];
  2235. } else if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
  2236. blob = &qla_fw_blobs[FW_ISP2300];
  2237. } else if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  2238. blob = &qla_fw_blobs[FW_ISP2322];
  2239. } else if (IS_QLA24XX(ha) || IS_QLA54XX(ha)) {
  2240. blob = &qla_fw_blobs[FW_ISP24XX];
  2241. }
  2242. down(&qla_fw_lock);
  2243. if (blob->fw)
  2244. goto out;
  2245. if (request_firmware(&blob->fw, blob->name, &ha->pdev->dev)) {
  2246. DEBUG2(printk("scsi(%ld): Failed to load firmware image "
  2247. "(%s).\n", ha->host_no, blob->name));
  2248. blob->fw = NULL;
  2249. blob = NULL;
  2250. goto out;
  2251. }
  2252. out:
  2253. up(&qla_fw_lock);
  2254. return blob;
  2255. }
  2256. static void
  2257. qla2x00_release_firmware(void)
  2258. {
  2259. int idx;
  2260. down(&qla_fw_lock);
  2261. for (idx = 0; idx < FW_BLOBS; idx++)
  2262. if (qla_fw_blobs[idx].fw)
  2263. release_firmware(qla_fw_blobs[idx].fw);
  2264. up(&qla_fw_lock);
  2265. }
  2266. static struct pci_device_id qla2xxx_pci_tbl[] = {
  2267. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2100) },
  2268. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2200) },
  2269. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2300) },
  2270. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2312) },
  2271. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2322) },
  2272. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP6312) },
  2273. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP6322) },
  2274. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2422) },
  2275. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2432) },
  2276. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP5422) },
  2277. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP5432) },
  2278. { 0 },
  2279. };
  2280. MODULE_DEVICE_TABLE(pci, qla2xxx_pci_tbl);
  2281. static struct pci_driver qla2xxx_pci_driver = {
  2282. .name = QLA2XXX_DRIVER_NAME,
  2283. .driver = {
  2284. .owner = THIS_MODULE,
  2285. },
  2286. .id_table = qla2xxx_pci_tbl,
  2287. .probe = qla2x00_probe_one,
  2288. .remove = __devexit_p(qla2x00_remove_one),
  2289. };
  2290. /**
  2291. * qla2x00_module_init - Module initialization.
  2292. **/
  2293. static int __init
  2294. qla2x00_module_init(void)
  2295. {
  2296. int ret = 0;
  2297. /* Allocate cache for SRBs. */
  2298. srb_cachep = kmem_cache_create("qla2xxx_srbs", sizeof(srb_t), 0,
  2299. SLAB_HWCACHE_ALIGN, NULL, NULL);
  2300. if (srb_cachep == NULL) {
  2301. printk(KERN_ERR
  2302. "qla2xxx: Unable to allocate SRB cache...Failing load!\n");
  2303. return -ENOMEM;
  2304. }
  2305. /* Derive version string. */
  2306. strcpy(qla2x00_version_str, QLA2XXX_VERSION);
  2307. if (ql2xextended_error_logging)
  2308. strcat(qla2x00_version_str, "-debug");
  2309. qla2xxx_transport_template =
  2310. fc_attach_transport(&qla2xxx_transport_functions);
  2311. if (!qla2xxx_transport_template) {
  2312. kmem_cache_destroy(srb_cachep);
  2313. return -ENODEV;
  2314. }
  2315. qla2xxx_transport_vport_template =
  2316. fc_attach_transport(&qla2xxx_transport_vport_functions);
  2317. if (!qla2xxx_transport_vport_template) {
  2318. kmem_cache_destroy(srb_cachep);
  2319. fc_release_transport(qla2xxx_transport_template);
  2320. return -ENODEV;
  2321. }
  2322. printk(KERN_INFO "QLogic Fibre Channel HBA Driver\n");
  2323. ret = pci_register_driver(&qla2xxx_pci_driver);
  2324. if (ret) {
  2325. kmem_cache_destroy(srb_cachep);
  2326. fc_release_transport(qla2xxx_transport_template);
  2327. fc_release_transport(qla2xxx_transport_vport_template);
  2328. }
  2329. return ret;
  2330. }
  2331. /**
  2332. * qla2x00_module_exit - Module cleanup.
  2333. **/
  2334. static void __exit
  2335. qla2x00_module_exit(void)
  2336. {
  2337. pci_unregister_driver(&qla2xxx_pci_driver);
  2338. qla2x00_release_firmware();
  2339. kmem_cache_destroy(srb_cachep);
  2340. fc_release_transport(qla2xxx_transport_template);
  2341. fc_release_transport(qla2xxx_transport_vport_template);
  2342. }
  2343. module_init(qla2x00_module_init);
  2344. module_exit(qla2x00_module_exit);
  2345. MODULE_AUTHOR("QLogic Corporation");
  2346. MODULE_DESCRIPTION("QLogic Fibre Channel HBA Driver");
  2347. MODULE_LICENSE("GPL");
  2348. MODULE_VERSION(QLA2XXX_VERSION);
  2349. MODULE_FIRMWARE(FW_FILE_ISP21XX);
  2350. MODULE_FIRMWARE(FW_FILE_ISP22XX);
  2351. MODULE_FIRMWARE(FW_FILE_ISP2300);
  2352. MODULE_FIRMWARE(FW_FILE_ISP2322);
  2353. MODULE_FIRMWARE(FW_FILE_ISP24XX);