ql4_os.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654
  1. /*
  2. * QLogic iSCSI HBA Driver
  3. * Copyright (c) 2003-2010 QLogic Corporation
  4. *
  5. * See LICENSE.qla4xxx for copyright and licensing details.
  6. */
  7. #include <linux/moduleparam.h>
  8. #include <linux/slab.h>
  9. #include <scsi/scsi_tcq.h>
  10. #include <scsi/scsicam.h>
  11. #include "ql4_def.h"
  12. #include "ql4_version.h"
  13. #include "ql4_glbl.h"
  14. #include "ql4_dbg.h"
  15. #include "ql4_inline.h"
  16. /*
  17. * Driver version
  18. */
  19. static char qla4xxx_version_str[40];
  20. /*
  21. * SRB allocation cache
  22. */
  23. static struct kmem_cache *srb_cachep;
  24. /*
  25. * Module parameter information and variables
  26. */
  27. int ql4xdontresethba = 0;
  28. module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
  29. MODULE_PARM_DESC(ql4xdontresethba,
  30. "Don't reset the HBA for driver recovery \n"
  31. " 0 - It will reset HBA (Default)\n"
  32. " 1 - It will NOT reset HBA");
  33. int ql4xextended_error_logging = 0; /* 0 = off, 1 = log errors */
  34. module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
  35. MODULE_PARM_DESC(ql4xextended_error_logging,
  36. "Option to enable extended error logging, "
  37. "Default is 0 - no logging, 1 - debug logging");
  38. int ql4xenablemsix = 1;
  39. module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
  40. MODULE_PARM_DESC(ql4xenablemsix,
  41. "Set to enable MSI or MSI-X interrupt mechanism.\n"
  42. " 0 = enable INTx interrupt mechanism.\n"
  43. " 1 = enable MSI-X interrupt mechanism (Default).\n"
  44. " 2 = enable MSI interrupt mechanism.");
  45. #define QL4_DEF_QDEPTH 32
  46. static int ql4xmaxqdepth = QL4_DEF_QDEPTH;
  47. module_param(ql4xmaxqdepth, int, S_IRUGO | S_IWUSR);
  48. MODULE_PARM_DESC(ql4xmaxqdepth,
  49. "Maximum queue depth to report for target devices.\n"
  50. " Default: 32.");
  51. static int ql4xsess_recovery_tmo = QL4_SESS_RECOVERY_TMO;
  52. module_param(ql4xsess_recovery_tmo, int, S_IRUGO);
  53. MODULE_PARM_DESC(ql4xsess_recovery_tmo,
  54. "Target Session Recovery Timeout.\n"
  55. " Default: 30 sec.");
  56. /*
  57. * SCSI host template entry points
  58. */
  59. static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
  60. /*
  61. * iSCSI template entry points
  62. */
  63. static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
  64. enum iscsi_tgt_dscvr type, uint32_t enable,
  65. struct sockaddr *dst_addr);
  66. static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
  67. enum iscsi_param param, char *buf);
  68. static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
  69. enum iscsi_param param, char *buf);
  70. static int qla4xxx_host_get_param(struct Scsi_Host *shost,
  71. enum iscsi_host_param param, char *buf);
  72. static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session);
  73. static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
  74. /*
  75. * SCSI host template entry points
  76. */
  77. static int qla4xxx_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
  78. static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
  79. static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
  80. static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
  81. static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
  82. static int qla4xxx_slave_alloc(struct scsi_device *device);
  83. static int qla4xxx_slave_configure(struct scsi_device *device);
  84. static void qla4xxx_slave_destroy(struct scsi_device *sdev);
  85. static void qla4xxx_scan_start(struct Scsi_Host *shost);
  86. static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
  87. QLA82XX_LEGACY_INTR_CONFIG;
  88. static struct scsi_host_template qla4xxx_driver_template = {
  89. .module = THIS_MODULE,
  90. .name = DRIVER_NAME,
  91. .proc_name = DRIVER_NAME,
  92. .queuecommand = qla4xxx_queuecommand,
  93. .eh_abort_handler = qla4xxx_eh_abort,
  94. .eh_device_reset_handler = qla4xxx_eh_device_reset,
  95. .eh_target_reset_handler = qla4xxx_eh_target_reset,
  96. .eh_host_reset_handler = qla4xxx_eh_host_reset,
  97. .eh_timed_out = qla4xxx_eh_cmd_timed_out,
  98. .slave_configure = qla4xxx_slave_configure,
  99. .slave_alloc = qla4xxx_slave_alloc,
  100. .slave_destroy = qla4xxx_slave_destroy,
  101. .scan_finished = iscsi_scan_finished,
  102. .scan_start = qla4xxx_scan_start,
  103. .this_id = -1,
  104. .cmd_per_lun = 3,
  105. .use_clustering = ENABLE_CLUSTERING,
  106. .sg_tablesize = SG_ALL,
  107. .max_sectors = 0xFFFF,
  108. };
  109. static struct iscsi_transport qla4xxx_iscsi_transport = {
  110. .owner = THIS_MODULE,
  111. .name = DRIVER_NAME,
  112. .caps = CAP_FW_DB | CAP_SENDTARGETS_OFFLOAD |
  113. CAP_DATA_PATH_OFFLOAD,
  114. .param_mask = ISCSI_CONN_PORT | ISCSI_CONN_ADDRESS |
  115. ISCSI_TARGET_NAME | ISCSI_TPGT |
  116. ISCSI_TARGET_ALIAS,
  117. .host_param_mask = ISCSI_HOST_HWADDRESS |
  118. ISCSI_HOST_IPADDRESS |
  119. ISCSI_HOST_INITIATOR_NAME,
  120. .tgt_dscvr = qla4xxx_tgt_dscvr,
  121. .get_conn_param = qla4xxx_conn_get_param,
  122. .get_session_param = qla4xxx_sess_get_param,
  123. .get_host_param = qla4xxx_host_get_param,
  124. .session_recovery_timedout = qla4xxx_recovery_timedout,
  125. };
  126. static struct scsi_transport_template *qla4xxx_scsi_transport;
  127. static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
  128. {
  129. struct iscsi_cls_session *session;
  130. struct ddb_entry *ddb_entry;
  131. session = starget_to_session(scsi_target(sc->device));
  132. ddb_entry = session->dd_data;
  133. /* if we are not logged in then the LLD is going to clean up the cmd */
  134. if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
  135. return BLK_EH_RESET_TIMER;
  136. else
  137. return BLK_EH_NOT_HANDLED;
  138. }
  139. static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session)
  140. {
  141. struct ddb_entry *ddb_entry = session->dd_data;
  142. struct scsi_qla_host *ha = ddb_entry->ha;
  143. if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
  144. atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
  145. DEBUG2(printk("scsi%ld: %s: ddb [%d] session recovery timeout "
  146. "of (%d) secs exhausted, marking device DEAD.\n",
  147. ha->host_no, __func__, ddb_entry->fw_ddb_index,
  148. ddb_entry->sess->recovery_tmo));
  149. }
  150. }
  151. static int qla4xxx_host_get_param(struct Scsi_Host *shost,
  152. enum iscsi_host_param param, char *buf)
  153. {
  154. struct scsi_qla_host *ha = to_qla_host(shost);
  155. int len;
  156. switch (param) {
  157. case ISCSI_HOST_PARAM_HWADDRESS:
  158. len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
  159. break;
  160. case ISCSI_HOST_PARAM_IPADDRESS:
  161. len = sprintf(buf, "%d.%d.%d.%d\n", ha->ip_address[0],
  162. ha->ip_address[1], ha->ip_address[2],
  163. ha->ip_address[3]);
  164. break;
  165. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  166. len = sprintf(buf, "%s\n", ha->name_string);
  167. break;
  168. default:
  169. return -ENOSYS;
  170. }
  171. return len;
  172. }
  173. static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
  174. enum iscsi_param param, char *buf)
  175. {
  176. struct ddb_entry *ddb_entry = sess->dd_data;
  177. int len;
  178. switch (param) {
  179. case ISCSI_PARAM_TARGET_NAME:
  180. len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
  181. ddb_entry->iscsi_name);
  182. break;
  183. case ISCSI_PARAM_TPGT:
  184. len = sprintf(buf, "%u\n", ddb_entry->tpgt);
  185. break;
  186. case ISCSI_PARAM_TARGET_ALIAS:
  187. len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
  188. ddb_entry->iscsi_alias);
  189. break;
  190. default:
  191. return -ENOSYS;
  192. }
  193. return len;
  194. }
  195. static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
  196. enum iscsi_param param, char *buf)
  197. {
  198. struct iscsi_cls_session *session;
  199. struct ddb_entry *ddb_entry;
  200. int len;
  201. session = iscsi_dev_to_session(conn->dev.parent);
  202. ddb_entry = session->dd_data;
  203. switch (param) {
  204. case ISCSI_PARAM_CONN_PORT:
  205. len = sprintf(buf, "%hu\n", ddb_entry->port);
  206. break;
  207. case ISCSI_PARAM_CONN_ADDRESS:
  208. /* TODO: what are the ipv6 bits */
  209. len = sprintf(buf, "%pI4\n", &ddb_entry->ip_addr);
  210. break;
  211. default:
  212. return -ENOSYS;
  213. }
  214. return len;
  215. }
  216. static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
  217. enum iscsi_tgt_dscvr type, uint32_t enable,
  218. struct sockaddr *dst_addr)
  219. {
  220. struct scsi_qla_host *ha;
  221. struct sockaddr_in *addr;
  222. struct sockaddr_in6 *addr6;
  223. int ret = 0;
  224. ha = (struct scsi_qla_host *) shost->hostdata;
  225. switch (type) {
  226. case ISCSI_TGT_DSCVR_SEND_TARGETS:
  227. if (dst_addr->sa_family == AF_INET) {
  228. addr = (struct sockaddr_in *)dst_addr;
  229. if (qla4xxx_send_tgts(ha, (char *)&addr->sin_addr,
  230. addr->sin_port) != QLA_SUCCESS)
  231. ret = -EIO;
  232. } else if (dst_addr->sa_family == AF_INET6) {
  233. /*
  234. * TODO: fix qla4xxx_send_tgts
  235. */
  236. addr6 = (struct sockaddr_in6 *)dst_addr;
  237. if (qla4xxx_send_tgts(ha, (char *)&addr6->sin6_addr,
  238. addr6->sin6_port) != QLA_SUCCESS)
  239. ret = -EIO;
  240. } else
  241. ret = -ENOSYS;
  242. break;
  243. default:
  244. ret = -ENOSYS;
  245. }
  246. return ret;
  247. }
  248. void qla4xxx_destroy_sess(struct ddb_entry *ddb_entry)
  249. {
  250. if (!ddb_entry->sess)
  251. return;
  252. if (ddb_entry->conn) {
  253. atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
  254. iscsi_remove_session(ddb_entry->sess);
  255. }
  256. iscsi_free_session(ddb_entry->sess);
  257. }
  258. int qla4xxx_add_sess(struct ddb_entry *ddb_entry)
  259. {
  260. int err;
  261. ddb_entry->sess->recovery_tmo = ql4xsess_recovery_tmo;
  262. err = iscsi_add_session(ddb_entry->sess, ddb_entry->fw_ddb_index);
  263. if (err) {
  264. DEBUG2(printk(KERN_ERR "Could not add session.\n"));
  265. return err;
  266. }
  267. ddb_entry->conn = iscsi_create_conn(ddb_entry->sess, 0, 0);
  268. if (!ddb_entry->conn) {
  269. iscsi_remove_session(ddb_entry->sess);
  270. DEBUG2(printk(KERN_ERR "Could not add connection.\n"));
  271. return -ENOMEM;
  272. }
  273. /* finally ready to go */
  274. iscsi_unblock_session(ddb_entry->sess);
  275. return 0;
  276. }
  277. struct ddb_entry *qla4xxx_alloc_sess(struct scsi_qla_host *ha)
  278. {
  279. struct ddb_entry *ddb_entry;
  280. struct iscsi_cls_session *sess;
  281. sess = iscsi_alloc_session(ha->host, &qla4xxx_iscsi_transport,
  282. sizeof(struct ddb_entry));
  283. if (!sess)
  284. return NULL;
  285. ddb_entry = sess->dd_data;
  286. memset(ddb_entry, 0, sizeof(*ddb_entry));
  287. ddb_entry->ha = ha;
  288. ddb_entry->sess = sess;
  289. return ddb_entry;
  290. }
  291. static void qla4xxx_scan_start(struct Scsi_Host *shost)
  292. {
  293. struct scsi_qla_host *ha = shost_priv(shost);
  294. struct ddb_entry *ddb_entry, *ddbtemp;
  295. /* finish setup of sessions that were already setup in firmware */
  296. list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
  297. if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE)
  298. qla4xxx_add_sess(ddb_entry);
  299. }
  300. }
  301. /*
  302. * Timer routines
  303. */
  304. static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
  305. unsigned long interval)
  306. {
  307. DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
  308. __func__, ha->host->host_no));
  309. init_timer(&ha->timer);
  310. ha->timer.expires = jiffies + interval * HZ;
  311. ha->timer.data = (unsigned long)ha;
  312. ha->timer.function = (void (*)(unsigned long))func;
  313. add_timer(&ha->timer);
  314. ha->timer_active = 1;
  315. }
  316. static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
  317. {
  318. del_timer_sync(&ha->timer);
  319. ha->timer_active = 0;
  320. }
  321. /***
  322. * qla4xxx_mark_device_missing - mark a device as missing.
  323. * @ha: Pointer to host adapter structure.
  324. * @ddb_entry: Pointer to device database entry
  325. *
  326. * This routine marks a device missing and close connection.
  327. **/
  328. void qla4xxx_mark_device_missing(struct scsi_qla_host *ha,
  329. struct ddb_entry *ddb_entry)
  330. {
  331. if ((atomic_read(&ddb_entry->state) != DDB_STATE_DEAD)) {
  332. atomic_set(&ddb_entry->state, DDB_STATE_MISSING);
  333. DEBUG2(printk("scsi%ld: ddb [%d] marked MISSING\n",
  334. ha->host_no, ddb_entry->fw_ddb_index));
  335. } else
  336. DEBUG2(printk("scsi%ld: ddb [%d] DEAD\n", ha->host_no,
  337. ddb_entry->fw_ddb_index))
  338. iscsi_block_session(ddb_entry->sess);
  339. iscsi_conn_error_event(ddb_entry->conn, ISCSI_ERR_CONN_FAILED);
  340. }
  341. /**
  342. * qla4xxx_mark_all_devices_missing - mark all devices as missing.
  343. * @ha: Pointer to host adapter structure.
  344. *
  345. * This routine marks a device missing and resets the relogin retry count.
  346. **/
  347. void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
  348. {
  349. struct ddb_entry *ddb_entry, *ddbtemp;
  350. list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
  351. qla4xxx_mark_device_missing(ha, ddb_entry);
  352. }
  353. }
  354. static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
  355. struct ddb_entry *ddb_entry,
  356. struct scsi_cmnd *cmd,
  357. void (*done)(struct scsi_cmnd *))
  358. {
  359. struct srb *srb;
  360. srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
  361. if (!srb)
  362. return srb;
  363. kref_init(&srb->srb_ref);
  364. srb->ha = ha;
  365. srb->ddb = ddb_entry;
  366. srb->cmd = cmd;
  367. srb->flags = 0;
  368. CMD_SP(cmd) = (void *)srb;
  369. cmd->scsi_done = done;
  370. return srb;
  371. }
  372. static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
  373. {
  374. struct scsi_cmnd *cmd = srb->cmd;
  375. if (srb->flags & SRB_DMA_VALID) {
  376. scsi_dma_unmap(cmd);
  377. srb->flags &= ~SRB_DMA_VALID;
  378. }
  379. CMD_SP(cmd) = NULL;
  380. }
  381. void qla4xxx_srb_compl(struct kref *ref)
  382. {
  383. struct srb *srb = container_of(ref, struct srb, srb_ref);
  384. struct scsi_cmnd *cmd = srb->cmd;
  385. struct scsi_qla_host *ha = srb->ha;
  386. qla4xxx_srb_free_dma(ha, srb);
  387. mempool_free(srb, ha->srb_mempool);
  388. cmd->scsi_done(cmd);
  389. }
  390. /**
  391. * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
  392. * @cmd: Pointer to Linux's SCSI command structure
  393. * @done_fn: Function that the driver calls to notify the SCSI mid-layer
  394. * that the command has been processed.
  395. *
  396. * Remarks:
  397. * This routine is invoked by Linux to send a SCSI command to the driver.
  398. * The mid-level driver tries to ensure that queuecommand never gets
  399. * invoked concurrently with itself or the interrupt handler (although
  400. * the interrupt handler may call this routine as part of request-
  401. * completion handling). Unfortunely, it sometimes calls the scheduler
  402. * in interrupt context which is a big NO! NO!.
  403. **/
  404. static int qla4xxx_queuecommand_lck(struct scsi_cmnd *cmd,
  405. void (*done)(struct scsi_cmnd *))
  406. {
  407. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  408. struct ddb_entry *ddb_entry = cmd->device->hostdata;
  409. struct iscsi_cls_session *sess = ddb_entry->sess;
  410. struct srb *srb;
  411. int rval;
  412. if (test_bit(AF_EEH_BUSY, &ha->flags)) {
  413. if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
  414. cmd->result = DID_NO_CONNECT << 16;
  415. else
  416. cmd->result = DID_REQUEUE << 16;
  417. goto qc_fail_command;
  418. }
  419. if (!sess) {
  420. cmd->result = DID_IMM_RETRY << 16;
  421. goto qc_fail_command;
  422. }
  423. rval = iscsi_session_chkready(sess);
  424. if (rval) {
  425. cmd->result = rval;
  426. goto qc_fail_command;
  427. }
  428. if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
  429. if (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD) {
  430. cmd->result = DID_NO_CONNECT << 16;
  431. goto qc_fail_command;
  432. }
  433. return SCSI_MLQUEUE_TARGET_BUSY;
  434. }
  435. if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
  436. test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
  437. test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  438. test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
  439. test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
  440. !test_bit(AF_ONLINE, &ha->flags) ||
  441. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
  442. goto qc_host_busy;
  443. spin_unlock_irq(ha->host->host_lock);
  444. srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd, done);
  445. if (!srb)
  446. goto qc_host_busy_lock;
  447. rval = qla4xxx_send_command_to_isp(ha, srb);
  448. if (rval != QLA_SUCCESS)
  449. goto qc_host_busy_free_sp;
  450. spin_lock_irq(ha->host->host_lock);
  451. return 0;
  452. qc_host_busy_free_sp:
  453. qla4xxx_srb_free_dma(ha, srb);
  454. mempool_free(srb, ha->srb_mempool);
  455. qc_host_busy_lock:
  456. spin_lock_irq(ha->host->host_lock);
  457. qc_host_busy:
  458. return SCSI_MLQUEUE_HOST_BUSY;
  459. qc_fail_command:
  460. done(cmd);
  461. return 0;
  462. }
  463. static DEF_SCSI_QCMD(qla4xxx_queuecommand)
  464. /**
  465. * qla4xxx_mem_free - frees memory allocated to adapter
  466. * @ha: Pointer to host adapter structure.
  467. *
  468. * Frees memory previously allocated by qla4xxx_mem_alloc
  469. **/
  470. static void qla4xxx_mem_free(struct scsi_qla_host *ha)
  471. {
  472. if (ha->queues)
  473. dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
  474. ha->queues_dma);
  475. ha->queues_len = 0;
  476. ha->queues = NULL;
  477. ha->queues_dma = 0;
  478. ha->request_ring = NULL;
  479. ha->request_dma = 0;
  480. ha->response_ring = NULL;
  481. ha->response_dma = 0;
  482. ha->shadow_regs = NULL;
  483. ha->shadow_regs_dma = 0;
  484. /* Free srb pool. */
  485. if (ha->srb_mempool)
  486. mempool_destroy(ha->srb_mempool);
  487. ha->srb_mempool = NULL;
  488. /* release io space registers */
  489. if (is_qla8022(ha)) {
  490. if (ha->nx_pcibase)
  491. iounmap(
  492. (struct device_reg_82xx __iomem *)ha->nx_pcibase);
  493. } else if (ha->reg)
  494. iounmap(ha->reg);
  495. pci_release_regions(ha->pdev);
  496. }
  497. /**
  498. * qla4xxx_mem_alloc - allocates memory for use by adapter.
  499. * @ha: Pointer to host adapter structure
  500. *
  501. * Allocates DMA memory for request and response queues. Also allocates memory
  502. * for srbs.
  503. **/
  504. static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
  505. {
  506. unsigned long align;
  507. /* Allocate contiguous block of DMA memory for queues. */
  508. ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
  509. (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
  510. sizeof(struct shadow_regs) +
  511. MEM_ALIGN_VALUE +
  512. (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
  513. ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
  514. &ha->queues_dma, GFP_KERNEL);
  515. if (ha->queues == NULL) {
  516. ql4_printk(KERN_WARNING, ha,
  517. "Memory Allocation failed - queues.\n");
  518. goto mem_alloc_error_exit;
  519. }
  520. memset(ha->queues, 0, ha->queues_len);
  521. /*
  522. * As per RISC alignment requirements -- the bus-address must be a
  523. * multiple of the request-ring size (in bytes).
  524. */
  525. align = 0;
  526. if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
  527. align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
  528. (MEM_ALIGN_VALUE - 1));
  529. /* Update request and response queue pointers. */
  530. ha->request_dma = ha->queues_dma + align;
  531. ha->request_ring = (struct queue_entry *) (ha->queues + align);
  532. ha->response_dma = ha->queues_dma + align +
  533. (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
  534. ha->response_ring = (struct queue_entry *) (ha->queues + align +
  535. (REQUEST_QUEUE_DEPTH *
  536. QUEUE_SIZE));
  537. ha->shadow_regs_dma = ha->queues_dma + align +
  538. (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
  539. (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
  540. ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
  541. (REQUEST_QUEUE_DEPTH *
  542. QUEUE_SIZE) +
  543. (RESPONSE_QUEUE_DEPTH *
  544. QUEUE_SIZE));
  545. /* Allocate memory for srb pool. */
  546. ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
  547. mempool_free_slab, srb_cachep);
  548. if (ha->srb_mempool == NULL) {
  549. ql4_printk(KERN_WARNING, ha,
  550. "Memory Allocation failed - SRB Pool.\n");
  551. goto mem_alloc_error_exit;
  552. }
  553. return QLA_SUCCESS;
  554. mem_alloc_error_exit:
  555. qla4xxx_mem_free(ha);
  556. return QLA_ERROR;
  557. }
  558. /**
  559. * qla4_8xxx_check_fw_alive - Check firmware health
  560. * @ha: Pointer to host adapter structure.
  561. *
  562. * Context: Interrupt
  563. **/
  564. static void qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
  565. {
  566. uint32_t fw_heartbeat_counter, halt_status;
  567. fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
  568. /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
  569. if (fw_heartbeat_counter == 0xffffffff) {
  570. DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
  571. "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
  572. ha->host_no, __func__));
  573. return;
  574. }
  575. if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
  576. ha->seconds_since_last_heartbeat++;
  577. /* FW not alive after 2 seconds */
  578. if (ha->seconds_since_last_heartbeat == 2) {
  579. ha->seconds_since_last_heartbeat = 0;
  580. halt_status = qla4_8xxx_rd_32(ha,
  581. QLA82XX_PEG_HALT_STATUS1);
  582. ql4_printk(KERN_INFO, ha,
  583. "scsi(%ld): %s, Dumping hw/fw registers:\n "
  584. " PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2:"
  585. " 0x%x,\n PEG_NET_0_PC: 0x%x, PEG_NET_1_PC:"
  586. " 0x%x,\n PEG_NET_2_PC: 0x%x, PEG_NET_3_PC:"
  587. " 0x%x,\n PEG_NET_4_PC: 0x%x\n",
  588. ha->host_no, __func__, halt_status,
  589. qla4_8xxx_rd_32(ha,
  590. QLA82XX_PEG_HALT_STATUS2),
  591. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_0 +
  592. 0x3c),
  593. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_1 +
  594. 0x3c),
  595. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_2 +
  596. 0x3c),
  597. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_3 +
  598. 0x3c),
  599. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_4 +
  600. 0x3c));
  601. /* Since we cannot change dev_state in interrupt
  602. * context, set appropriate DPC flag then wakeup
  603. * DPC */
  604. if (halt_status & HALT_STATUS_UNRECOVERABLE)
  605. set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
  606. else {
  607. printk("scsi%ld: %s: detect abort needed!\n",
  608. ha->host_no, __func__);
  609. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  610. }
  611. qla4xxx_wake_dpc(ha);
  612. qla4xxx_mailbox_premature_completion(ha);
  613. }
  614. } else
  615. ha->seconds_since_last_heartbeat = 0;
  616. ha->fw_heartbeat_counter = fw_heartbeat_counter;
  617. }
  618. /**
  619. * qla4_8xxx_watchdog - Poll dev state
  620. * @ha: Pointer to host adapter structure.
  621. *
  622. * Context: Interrupt
  623. **/
  624. void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
  625. {
  626. uint32_t dev_state;
  627. dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
  628. /* don't poll if reset is going on */
  629. if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
  630. test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  631. test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags))) {
  632. if (dev_state == QLA82XX_DEV_NEED_RESET &&
  633. !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
  634. if (!ql4xdontresethba) {
  635. ql4_printk(KERN_INFO, ha, "%s: HW State: "
  636. "NEED RESET!\n", __func__);
  637. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  638. qla4xxx_wake_dpc(ha);
  639. qla4xxx_mailbox_premature_completion(ha);
  640. }
  641. } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
  642. !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
  643. ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
  644. __func__);
  645. set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
  646. qla4xxx_wake_dpc(ha);
  647. } else {
  648. /* Check firmware health */
  649. qla4_8xxx_check_fw_alive(ha);
  650. }
  651. }
  652. }
  653. /**
  654. * qla4xxx_timer - checks every second for work to do.
  655. * @ha: Pointer to host adapter structure.
  656. **/
  657. static void qla4xxx_timer(struct scsi_qla_host *ha)
  658. {
  659. struct ddb_entry *ddb_entry, *dtemp;
  660. int start_dpc = 0;
  661. uint16_t w;
  662. /* If we are in the middle of AER/EEH processing
  663. * skip any processing and reschedule the timer
  664. */
  665. if (test_bit(AF_EEH_BUSY, &ha->flags)) {
  666. mod_timer(&ha->timer, jiffies + HZ);
  667. return;
  668. }
  669. /* Hardware read to trigger an EEH error during mailbox waits. */
  670. if (!pci_channel_offline(ha->pdev))
  671. pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
  672. if (is_qla8022(ha)) {
  673. qla4_8xxx_watchdog(ha);
  674. }
  675. /* Search for relogin's to time-out and port down retry. */
  676. list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
  677. /* Count down time between sending relogins */
  678. if (adapter_up(ha) &&
  679. !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
  680. atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
  681. if (atomic_read(&ddb_entry->retry_relogin_timer) !=
  682. INVALID_ENTRY) {
  683. if (atomic_read(&ddb_entry->retry_relogin_timer)
  684. == 0) {
  685. atomic_set(&ddb_entry->
  686. retry_relogin_timer,
  687. INVALID_ENTRY);
  688. set_bit(DPC_RELOGIN_DEVICE,
  689. &ha->dpc_flags);
  690. set_bit(DF_RELOGIN, &ddb_entry->flags);
  691. DEBUG2(printk("scsi%ld: %s: ddb [%d]"
  692. " login device\n",
  693. ha->host_no, __func__,
  694. ddb_entry->fw_ddb_index));
  695. } else
  696. atomic_dec(&ddb_entry->
  697. retry_relogin_timer);
  698. }
  699. }
  700. /* Wait for relogin to timeout */
  701. if (atomic_read(&ddb_entry->relogin_timer) &&
  702. (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
  703. /*
  704. * If the relogin times out and the device is
  705. * still NOT ONLINE then try and relogin again.
  706. */
  707. if (atomic_read(&ddb_entry->state) !=
  708. DDB_STATE_ONLINE &&
  709. ddb_entry->fw_ddb_device_state ==
  710. DDB_DS_SESSION_FAILED) {
  711. /* Reset retry relogin timer */
  712. atomic_inc(&ddb_entry->relogin_retry_count);
  713. DEBUG2(printk("scsi%ld: ddb [%d] relogin"
  714. " timed out-retrying"
  715. " relogin (%d)\n",
  716. ha->host_no,
  717. ddb_entry->fw_ddb_index,
  718. atomic_read(&ddb_entry->
  719. relogin_retry_count))
  720. );
  721. start_dpc++;
  722. DEBUG(printk("scsi%ld:%d:%d: ddb [%d] "
  723. "initiate relogin after"
  724. " %d seconds\n",
  725. ha->host_no, ddb_entry->bus,
  726. ddb_entry->target,
  727. ddb_entry->fw_ddb_index,
  728. ddb_entry->default_time2wait + 4)
  729. );
  730. atomic_set(&ddb_entry->retry_relogin_timer,
  731. ddb_entry->default_time2wait + 4);
  732. }
  733. }
  734. }
  735. if (!is_qla8022(ha)) {
  736. /* Check for heartbeat interval. */
  737. if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
  738. ha->heartbeat_interval != 0) {
  739. ha->seconds_since_last_heartbeat++;
  740. if (ha->seconds_since_last_heartbeat >
  741. ha->heartbeat_interval + 2)
  742. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  743. }
  744. }
  745. /* Wakeup the dpc routine for this adapter, if needed. */
  746. if ((start_dpc ||
  747. test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  748. test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
  749. test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
  750. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
  751. test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
  752. test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
  753. test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
  754. test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
  755. test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
  756. test_bit(DPC_AEN, &ha->dpc_flags)) &&
  757. !test_bit(AF_DPC_SCHEDULED, &ha->flags) &&
  758. ha->dpc_thread) {
  759. DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
  760. " - dpc flags = 0x%lx\n",
  761. ha->host_no, __func__, ha->dpc_flags));
  762. qla4xxx_wake_dpc(ha);
  763. }
  764. /* Reschedule timer thread to call us back in one second */
  765. mod_timer(&ha->timer, jiffies + HZ);
  766. DEBUG2(ha->seconds_since_last_intr++);
  767. }
  768. /**
  769. * qla4xxx_cmd_wait - waits for all outstanding commands to complete
  770. * @ha: Pointer to host adapter structure.
  771. *
  772. * This routine stalls the driver until all outstanding commands are returned.
  773. * Caller must release the Hardware Lock prior to calling this routine.
  774. **/
  775. static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
  776. {
  777. uint32_t index = 0;
  778. unsigned long flags;
  779. struct scsi_cmnd *cmd;
  780. unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
  781. DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
  782. "complete\n", WAIT_CMD_TOV));
  783. while (!time_after_eq(jiffies, wtime)) {
  784. spin_lock_irqsave(&ha->hardware_lock, flags);
  785. /* Find a command that hasn't completed. */
  786. for (index = 0; index < ha->host->can_queue; index++) {
  787. cmd = scsi_host_find_tag(ha->host, index);
  788. /*
  789. * We cannot just check if the index is valid,
  790. * becase if we are run from the scsi eh, then
  791. * the scsi/block layer is going to prevent
  792. * the tag from being released.
  793. */
  794. if (cmd != NULL && CMD_SP(cmd))
  795. break;
  796. }
  797. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  798. /* If No Commands are pending, wait is complete */
  799. if (index == ha->host->can_queue)
  800. return QLA_SUCCESS;
  801. msleep(1000);
  802. }
  803. /* If we timed out on waiting for commands to come back
  804. * return ERROR. */
  805. return QLA_ERROR;
  806. }
  807. int qla4xxx_hw_reset(struct scsi_qla_host *ha)
  808. {
  809. uint32_t ctrl_status;
  810. unsigned long flags = 0;
  811. DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
  812. if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
  813. return QLA_ERROR;
  814. spin_lock_irqsave(&ha->hardware_lock, flags);
  815. /*
  816. * If the SCSI Reset Interrupt bit is set, clear it.
  817. * Otherwise, the Soft Reset won't work.
  818. */
  819. ctrl_status = readw(&ha->reg->ctrl_status);
  820. if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
  821. writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
  822. /* Issue Soft Reset */
  823. writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
  824. readl(&ha->reg->ctrl_status);
  825. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  826. return QLA_SUCCESS;
  827. }
  828. /**
  829. * qla4xxx_soft_reset - performs soft reset.
  830. * @ha: Pointer to host adapter structure.
  831. **/
  832. int qla4xxx_soft_reset(struct scsi_qla_host *ha)
  833. {
  834. uint32_t max_wait_time;
  835. unsigned long flags = 0;
  836. int status;
  837. uint32_t ctrl_status;
  838. status = qla4xxx_hw_reset(ha);
  839. if (status != QLA_SUCCESS)
  840. return status;
  841. status = QLA_ERROR;
  842. /* Wait until the Network Reset Intr bit is cleared */
  843. max_wait_time = RESET_INTR_TOV;
  844. do {
  845. spin_lock_irqsave(&ha->hardware_lock, flags);
  846. ctrl_status = readw(&ha->reg->ctrl_status);
  847. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  848. if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
  849. break;
  850. msleep(1000);
  851. } while ((--max_wait_time));
  852. if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
  853. DEBUG2(printk(KERN_WARNING
  854. "scsi%ld: Network Reset Intr not cleared by "
  855. "Network function, clearing it now!\n",
  856. ha->host_no));
  857. spin_lock_irqsave(&ha->hardware_lock, flags);
  858. writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
  859. readl(&ha->reg->ctrl_status);
  860. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  861. }
  862. /* Wait until the firmware tells us the Soft Reset is done */
  863. max_wait_time = SOFT_RESET_TOV;
  864. do {
  865. spin_lock_irqsave(&ha->hardware_lock, flags);
  866. ctrl_status = readw(&ha->reg->ctrl_status);
  867. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  868. if ((ctrl_status & CSR_SOFT_RESET) == 0) {
  869. status = QLA_SUCCESS;
  870. break;
  871. }
  872. msleep(1000);
  873. } while ((--max_wait_time));
  874. /*
  875. * Also, make sure that the SCSI Reset Interrupt bit has been cleared
  876. * after the soft reset has taken place.
  877. */
  878. spin_lock_irqsave(&ha->hardware_lock, flags);
  879. ctrl_status = readw(&ha->reg->ctrl_status);
  880. if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
  881. writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
  882. readl(&ha->reg->ctrl_status);
  883. }
  884. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  885. /* If soft reset fails then most probably the bios on other
  886. * function is also enabled.
  887. * Since the initialization is sequential the other fn
  888. * wont be able to acknowledge the soft reset.
  889. * Issue a force soft reset to workaround this scenario.
  890. */
  891. if (max_wait_time == 0) {
  892. /* Issue Force Soft Reset */
  893. spin_lock_irqsave(&ha->hardware_lock, flags);
  894. writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
  895. readl(&ha->reg->ctrl_status);
  896. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  897. /* Wait until the firmware tells us the Soft Reset is done */
  898. max_wait_time = SOFT_RESET_TOV;
  899. do {
  900. spin_lock_irqsave(&ha->hardware_lock, flags);
  901. ctrl_status = readw(&ha->reg->ctrl_status);
  902. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  903. if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
  904. status = QLA_SUCCESS;
  905. break;
  906. }
  907. msleep(1000);
  908. } while ((--max_wait_time));
  909. }
  910. return status;
  911. }
  912. /**
  913. * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
  914. * @ha: Pointer to host adapter structure.
  915. * @res: returned scsi status
  916. *
  917. * This routine is called just prior to a HARD RESET to return all
  918. * outstanding commands back to the Operating System.
  919. * Caller should make sure that the following locks are released
  920. * before this calling routine: Hardware lock, and io_request_lock.
  921. **/
  922. static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
  923. {
  924. struct srb *srb;
  925. int i;
  926. unsigned long flags;
  927. spin_lock_irqsave(&ha->hardware_lock, flags);
  928. for (i = 0; i < ha->host->can_queue; i++) {
  929. srb = qla4xxx_del_from_active_array(ha, i);
  930. if (srb != NULL) {
  931. srb->cmd->result = res;
  932. kref_put(&srb->srb_ref, qla4xxx_srb_compl);
  933. }
  934. }
  935. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  936. }
  937. void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
  938. {
  939. clear_bit(AF_ONLINE, &ha->flags);
  940. /* Disable the board */
  941. ql4_printk(KERN_INFO, ha, "Disabling the board\n");
  942. qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
  943. qla4xxx_mark_all_devices_missing(ha);
  944. clear_bit(AF_INIT_DONE, &ha->flags);
  945. }
  946. /**
  947. * qla4xxx_recover_adapter - recovers adapter after a fatal error
  948. * @ha: Pointer to host adapter structure.
  949. **/
  950. static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
  951. {
  952. int status = QLA_ERROR;
  953. uint8_t reset_chip = 0;
  954. /* Stall incoming I/O until we are done */
  955. scsi_block_requests(ha->host);
  956. clear_bit(AF_ONLINE, &ha->flags);
  957. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
  958. set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  959. if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
  960. reset_chip = 1;
  961. /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
  962. * do not reset adapter, jump to initialize_adapter */
  963. if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
  964. status = QLA_SUCCESS;
  965. goto recover_ha_init_adapter;
  966. }
  967. /* For the ISP-82xx adapter, issue a stop_firmware if invoked
  968. * from eh_host_reset or ioctl module */
  969. if (is_qla8022(ha) && !reset_chip &&
  970. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
  971. DEBUG2(ql4_printk(KERN_INFO, ha,
  972. "scsi%ld: %s - Performing stop_firmware...\n",
  973. ha->host_no, __func__));
  974. status = ha->isp_ops->reset_firmware(ha);
  975. if (status == QLA_SUCCESS) {
  976. if (!test_bit(AF_FW_RECOVERY, &ha->flags))
  977. qla4xxx_cmd_wait(ha);
  978. ha->isp_ops->disable_intrs(ha);
  979. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  980. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  981. } else {
  982. /* If the stop_firmware fails then
  983. * reset the entire chip */
  984. reset_chip = 1;
  985. clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  986. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  987. }
  988. }
  989. /* Issue full chip reset if recovering from a catastrophic error,
  990. * or if stop_firmware fails for ISP-82xx.
  991. * This is the default case for ISP-4xxx */
  992. if (!is_qla8022(ha) || reset_chip) {
  993. if (!test_bit(AF_FW_RECOVERY, &ha->flags))
  994. qla4xxx_cmd_wait(ha);
  995. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  996. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  997. DEBUG2(ql4_printk(KERN_INFO, ha,
  998. "scsi%ld: %s - Performing chip reset..\n",
  999. ha->host_no, __func__));
  1000. status = ha->isp_ops->reset_chip(ha);
  1001. }
  1002. /* Flush any pending ddb changed AENs */
  1003. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  1004. recover_ha_init_adapter:
  1005. /* Upon successful firmware/chip reset, re-initialize the adapter */
  1006. if (status == QLA_SUCCESS) {
  1007. /* For ISP-4xxx, force function 1 to always initialize
  1008. * before function 3 to prevent both funcions from
  1009. * stepping on top of the other */
  1010. if (!is_qla8022(ha) && (ha->mac_index == 3))
  1011. ssleep(6);
  1012. /* NOTE: AF_ONLINE flag set upon successful completion of
  1013. * qla4xxx_initialize_adapter */
  1014. status = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
  1015. }
  1016. /* Retry failed adapter initialization, if necessary
  1017. * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
  1018. * case to prevent ping-pong resets between functions */
  1019. if (!test_bit(AF_ONLINE, &ha->flags) &&
  1020. !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
  1021. /* Adapter initialization failed, see if we can retry
  1022. * resetting the ha.
  1023. * Since we don't want to block the DPC for too long
  1024. * with multiple resets in the same thread,
  1025. * utilize DPC to retry */
  1026. if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
  1027. ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
  1028. DEBUG2(printk("scsi%ld: recover adapter - retrying "
  1029. "(%d) more times\n", ha->host_no,
  1030. ha->retry_reset_ha_cnt));
  1031. set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  1032. status = QLA_ERROR;
  1033. } else {
  1034. if (ha->retry_reset_ha_cnt > 0) {
  1035. /* Schedule another Reset HA--DPC will retry */
  1036. ha->retry_reset_ha_cnt--;
  1037. DEBUG2(printk("scsi%ld: recover adapter - "
  1038. "retry remaining %d\n",
  1039. ha->host_no,
  1040. ha->retry_reset_ha_cnt));
  1041. status = QLA_ERROR;
  1042. }
  1043. if (ha->retry_reset_ha_cnt == 0) {
  1044. /* Recover adapter retries have been exhausted.
  1045. * Adapter DEAD */
  1046. DEBUG2(printk("scsi%ld: recover adapter "
  1047. "failed - board disabled\n",
  1048. ha->host_no));
  1049. qla4xxx_dead_adapter_cleanup(ha);
  1050. clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  1051. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  1052. clear_bit(DPC_RESET_HA_FW_CONTEXT,
  1053. &ha->dpc_flags);
  1054. status = QLA_ERROR;
  1055. }
  1056. }
  1057. } else {
  1058. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  1059. clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  1060. clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  1061. }
  1062. ha->adapter_error_count++;
  1063. if (test_bit(AF_ONLINE, &ha->flags))
  1064. ha->isp_ops->enable_intrs(ha);
  1065. scsi_unblock_requests(ha->host);
  1066. clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  1067. DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
  1068. status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
  1069. return status;
  1070. }
  1071. static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
  1072. {
  1073. struct ddb_entry *ddb_entry, *dtemp;
  1074. list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
  1075. if ((atomic_read(&ddb_entry->state) == DDB_STATE_MISSING) ||
  1076. (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD)) {
  1077. if (ddb_entry->fw_ddb_device_state ==
  1078. DDB_DS_SESSION_ACTIVE) {
  1079. atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
  1080. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
  1081. " marked ONLINE\n", ha->host_no, __func__,
  1082. ddb_entry->fw_ddb_index);
  1083. iscsi_unblock_session(ddb_entry->sess);
  1084. } else
  1085. qla4xxx_relogin_device(ha, ddb_entry);
  1086. }
  1087. }
  1088. }
  1089. void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
  1090. {
  1091. if (ha->dpc_thread &&
  1092. !test_bit(AF_DPC_SCHEDULED, &ha->flags)) {
  1093. set_bit(AF_DPC_SCHEDULED, &ha->flags);
  1094. queue_work(ha->dpc_thread, &ha->dpc_work);
  1095. }
  1096. }
  1097. /**
  1098. * qla4xxx_do_dpc - dpc routine
  1099. * @data: in our case pointer to adapter structure
  1100. *
  1101. * This routine is a task that is schedule by the interrupt handler
  1102. * to perform the background processing for interrupts. We put it
  1103. * on a task queue that is consumed whenever the scheduler runs; that's
  1104. * so you can do anything (i.e. put the process to sleep etc). In fact,
  1105. * the mid-level tries to sleep when it reaches the driver threshold
  1106. * "host->can_queue". This can cause a panic if we were in our interrupt code.
  1107. **/
  1108. static void qla4xxx_do_dpc(struct work_struct *work)
  1109. {
  1110. struct scsi_qla_host *ha =
  1111. container_of(work, struct scsi_qla_host, dpc_work);
  1112. struct ddb_entry *ddb_entry, *dtemp;
  1113. int status = QLA_ERROR;
  1114. DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
  1115. "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
  1116. ha->host_no, __func__, ha->flags, ha->dpc_flags))
  1117. /* Initialization not yet finished. Don't do anything yet. */
  1118. if (!test_bit(AF_INIT_DONE, &ha->flags))
  1119. goto do_dpc_exit;
  1120. if (test_bit(AF_EEH_BUSY, &ha->flags)) {
  1121. DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
  1122. ha->host_no, __func__, ha->flags));
  1123. goto do_dpc_exit;
  1124. }
  1125. if (is_qla8022(ha)) {
  1126. if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
  1127. qla4_8xxx_idc_lock(ha);
  1128. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  1129. QLA82XX_DEV_FAILED);
  1130. qla4_8xxx_idc_unlock(ha);
  1131. ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
  1132. qla4_8xxx_device_state_handler(ha);
  1133. }
  1134. if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
  1135. qla4_8xxx_need_qsnt_handler(ha);
  1136. }
  1137. }
  1138. if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
  1139. (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  1140. test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
  1141. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
  1142. if (ql4xdontresethba) {
  1143. DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
  1144. ha->host_no, __func__));
  1145. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  1146. clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
  1147. clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  1148. goto dpc_post_reset_ha;
  1149. }
  1150. if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
  1151. test_bit(DPC_RESET_HA, &ha->dpc_flags))
  1152. qla4xxx_recover_adapter(ha);
  1153. if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
  1154. uint8_t wait_time = RESET_INTR_TOV;
  1155. while ((readw(&ha->reg->ctrl_status) &
  1156. (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
  1157. if (--wait_time == 0)
  1158. break;
  1159. msleep(1000);
  1160. }
  1161. if (wait_time == 0)
  1162. DEBUG2(printk("scsi%ld: %s: SR|FSR "
  1163. "bit not cleared-- resetting\n",
  1164. ha->host_no, __func__));
  1165. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  1166. if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
  1167. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  1168. status = qla4xxx_recover_adapter(ha);
  1169. }
  1170. clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
  1171. if (status == QLA_SUCCESS)
  1172. ha->isp_ops->enable_intrs(ha);
  1173. }
  1174. }
  1175. dpc_post_reset_ha:
  1176. /* ---- process AEN? --- */
  1177. if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
  1178. qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
  1179. /* ---- Get DHCP IP Address? --- */
  1180. if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
  1181. qla4xxx_get_dhcp_ip_address(ha);
  1182. /* ---- link change? --- */
  1183. if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
  1184. if (!test_bit(AF_LINK_UP, &ha->flags)) {
  1185. /* ---- link down? --- */
  1186. qla4xxx_mark_all_devices_missing(ha);
  1187. } else {
  1188. /* ---- link up? --- *
  1189. * F/W will auto login to all devices ONLY ONCE after
  1190. * link up during driver initialization and runtime
  1191. * fatal error recovery. Therefore, the driver must
  1192. * manually relogin to devices when recovering from
  1193. * connection failures, logouts, expired KATO, etc. */
  1194. qla4xxx_relogin_all_devices(ha);
  1195. }
  1196. }
  1197. /* ---- relogin device? --- */
  1198. if (adapter_up(ha) &&
  1199. test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
  1200. list_for_each_entry_safe(ddb_entry, dtemp,
  1201. &ha->ddb_list, list) {
  1202. if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
  1203. atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
  1204. qla4xxx_relogin_device(ha, ddb_entry);
  1205. /*
  1206. * If mbx cmd times out there is no point
  1207. * in continuing further.
  1208. * With large no of targets this can hang
  1209. * the system.
  1210. */
  1211. if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
  1212. printk(KERN_WARNING "scsi%ld: %s: "
  1213. "need to reset hba\n",
  1214. ha->host_no, __func__);
  1215. break;
  1216. }
  1217. }
  1218. }
  1219. do_dpc_exit:
  1220. clear_bit(AF_DPC_SCHEDULED, &ha->flags);
  1221. }
  1222. /**
  1223. * qla4xxx_free_adapter - release the adapter
  1224. * @ha: pointer to adapter structure
  1225. **/
  1226. static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
  1227. {
  1228. if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
  1229. /* Turn-off interrupts on the card. */
  1230. ha->isp_ops->disable_intrs(ha);
  1231. }
  1232. /* Remove timer thread, if present */
  1233. if (ha->timer_active)
  1234. qla4xxx_stop_timer(ha);
  1235. /* Kill the kernel thread for this host */
  1236. if (ha->dpc_thread)
  1237. destroy_workqueue(ha->dpc_thread);
  1238. /* Put firmware in known state */
  1239. ha->isp_ops->reset_firmware(ha);
  1240. if (is_qla8022(ha)) {
  1241. qla4_8xxx_idc_lock(ha);
  1242. qla4_8xxx_clear_drv_active(ha);
  1243. qla4_8xxx_idc_unlock(ha);
  1244. }
  1245. /* Detach interrupts */
  1246. if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
  1247. qla4xxx_free_irqs(ha);
  1248. /* free extra memory */
  1249. qla4xxx_mem_free(ha);
  1250. }
  1251. int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
  1252. {
  1253. int status = 0;
  1254. uint8_t revision_id;
  1255. unsigned long mem_base, mem_len, db_base, db_len;
  1256. struct pci_dev *pdev = ha->pdev;
  1257. status = pci_request_regions(pdev, DRIVER_NAME);
  1258. if (status) {
  1259. printk(KERN_WARNING
  1260. "scsi(%ld) Failed to reserve PIO regions (%s) "
  1261. "status=%d\n", ha->host_no, pci_name(pdev), status);
  1262. goto iospace_error_exit;
  1263. }
  1264. pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
  1265. DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
  1266. __func__, revision_id));
  1267. ha->revision_id = revision_id;
  1268. /* remap phys address */
  1269. mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
  1270. mem_len = pci_resource_len(pdev, 0);
  1271. DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
  1272. __func__, mem_base, mem_len));
  1273. /* mapping of pcibase pointer */
  1274. ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
  1275. if (!ha->nx_pcibase) {
  1276. printk(KERN_ERR
  1277. "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
  1278. pci_release_regions(ha->pdev);
  1279. goto iospace_error_exit;
  1280. }
  1281. /* Mapping of IO base pointer, door bell read and write pointer */
  1282. /* mapping of IO base pointer */
  1283. ha->qla4_8xxx_reg =
  1284. (struct device_reg_82xx __iomem *)((uint8_t *)ha->nx_pcibase +
  1285. 0xbc000 + (ha->pdev->devfn << 11));
  1286. db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
  1287. db_len = pci_resource_len(pdev, 4);
  1288. ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
  1289. QLA82XX_CAM_RAM_DB2);
  1290. return 0;
  1291. iospace_error_exit:
  1292. return -ENOMEM;
  1293. }
  1294. /***
  1295. * qla4xxx_iospace_config - maps registers
  1296. * @ha: pointer to adapter structure
  1297. *
  1298. * This routines maps HBA's registers from the pci address space
  1299. * into the kernel virtual address space for memory mapped i/o.
  1300. **/
  1301. int qla4xxx_iospace_config(struct scsi_qla_host *ha)
  1302. {
  1303. unsigned long pio, pio_len, pio_flags;
  1304. unsigned long mmio, mmio_len, mmio_flags;
  1305. pio = pci_resource_start(ha->pdev, 0);
  1306. pio_len = pci_resource_len(ha->pdev, 0);
  1307. pio_flags = pci_resource_flags(ha->pdev, 0);
  1308. if (pio_flags & IORESOURCE_IO) {
  1309. if (pio_len < MIN_IOBASE_LEN) {
  1310. ql4_printk(KERN_WARNING, ha,
  1311. "Invalid PCI I/O region size\n");
  1312. pio = 0;
  1313. }
  1314. } else {
  1315. ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
  1316. pio = 0;
  1317. }
  1318. /* Use MMIO operations for all accesses. */
  1319. mmio = pci_resource_start(ha->pdev, 1);
  1320. mmio_len = pci_resource_len(ha->pdev, 1);
  1321. mmio_flags = pci_resource_flags(ha->pdev, 1);
  1322. if (!(mmio_flags & IORESOURCE_MEM)) {
  1323. ql4_printk(KERN_ERR, ha,
  1324. "region #0 not an MMIO resource, aborting\n");
  1325. goto iospace_error_exit;
  1326. }
  1327. if (mmio_len < MIN_IOBASE_LEN) {
  1328. ql4_printk(KERN_ERR, ha,
  1329. "Invalid PCI mem region size, aborting\n");
  1330. goto iospace_error_exit;
  1331. }
  1332. if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
  1333. ql4_printk(KERN_WARNING, ha,
  1334. "Failed to reserve PIO/MMIO regions\n");
  1335. goto iospace_error_exit;
  1336. }
  1337. ha->pio_address = pio;
  1338. ha->pio_length = pio_len;
  1339. ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
  1340. if (!ha->reg) {
  1341. ql4_printk(KERN_ERR, ha,
  1342. "cannot remap MMIO, aborting\n");
  1343. goto iospace_error_exit;
  1344. }
  1345. return 0;
  1346. iospace_error_exit:
  1347. return -ENOMEM;
  1348. }
  1349. static struct isp_operations qla4xxx_isp_ops = {
  1350. .iospace_config = qla4xxx_iospace_config,
  1351. .pci_config = qla4xxx_pci_config,
  1352. .disable_intrs = qla4xxx_disable_intrs,
  1353. .enable_intrs = qla4xxx_enable_intrs,
  1354. .start_firmware = qla4xxx_start_firmware,
  1355. .intr_handler = qla4xxx_intr_handler,
  1356. .interrupt_service_routine = qla4xxx_interrupt_service_routine,
  1357. .reset_chip = qla4xxx_soft_reset,
  1358. .reset_firmware = qla4xxx_hw_reset,
  1359. .queue_iocb = qla4xxx_queue_iocb,
  1360. .complete_iocb = qla4xxx_complete_iocb,
  1361. .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
  1362. .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
  1363. .get_sys_info = qla4xxx_get_sys_info,
  1364. };
  1365. static struct isp_operations qla4_8xxx_isp_ops = {
  1366. .iospace_config = qla4_8xxx_iospace_config,
  1367. .pci_config = qla4_8xxx_pci_config,
  1368. .disable_intrs = qla4_8xxx_disable_intrs,
  1369. .enable_intrs = qla4_8xxx_enable_intrs,
  1370. .start_firmware = qla4_8xxx_load_risc,
  1371. .intr_handler = qla4_8xxx_intr_handler,
  1372. .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
  1373. .reset_chip = qla4_8xxx_isp_reset,
  1374. .reset_firmware = qla4_8xxx_stop_firmware,
  1375. .queue_iocb = qla4_8xxx_queue_iocb,
  1376. .complete_iocb = qla4_8xxx_complete_iocb,
  1377. .rd_shdw_req_q_out = qla4_8xxx_rd_shdw_req_q_out,
  1378. .rd_shdw_rsp_q_in = qla4_8xxx_rd_shdw_rsp_q_in,
  1379. .get_sys_info = qla4_8xxx_get_sys_info,
  1380. };
  1381. uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
  1382. {
  1383. return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
  1384. }
  1385. uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
  1386. {
  1387. return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
  1388. }
  1389. uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
  1390. {
  1391. return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
  1392. }
  1393. uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
  1394. {
  1395. return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
  1396. }
  1397. /**
  1398. * qla4xxx_probe_adapter - callback function to probe HBA
  1399. * @pdev: pointer to pci_dev structure
  1400. * @pci_device_id: pointer to pci_device entry
  1401. *
  1402. * This routine will probe for Qlogic 4xxx iSCSI host adapters.
  1403. * It returns zero if successful. It also initializes all data necessary for
  1404. * the driver.
  1405. **/
  1406. static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
  1407. const struct pci_device_id *ent)
  1408. {
  1409. int ret = -ENODEV, status;
  1410. struct Scsi_Host *host;
  1411. struct scsi_qla_host *ha;
  1412. uint8_t init_retry_count = 0;
  1413. char buf[34];
  1414. struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
  1415. uint32_t dev_state;
  1416. if (pci_enable_device(pdev))
  1417. return -1;
  1418. host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
  1419. if (host == NULL) {
  1420. printk(KERN_WARNING
  1421. "qla4xxx: Couldn't allocate host from scsi layer!\n");
  1422. goto probe_disable_device;
  1423. }
  1424. /* Clear our data area */
  1425. ha = (struct scsi_qla_host *) host->hostdata;
  1426. memset(ha, 0, sizeof(*ha));
  1427. /* Save the information from PCI BIOS. */
  1428. ha->pdev = pdev;
  1429. ha->host = host;
  1430. ha->host_no = host->host_no;
  1431. pci_enable_pcie_error_reporting(pdev);
  1432. /* Setup Runtime configurable options */
  1433. if (is_qla8022(ha)) {
  1434. ha->isp_ops = &qla4_8xxx_isp_ops;
  1435. rwlock_init(&ha->hw_lock);
  1436. ha->qdr_sn_window = -1;
  1437. ha->ddr_mn_window = -1;
  1438. ha->curr_window = 255;
  1439. ha->func_num = PCI_FUNC(ha->pdev->devfn);
  1440. nx_legacy_intr = &legacy_intr[ha->func_num];
  1441. ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
  1442. ha->nx_legacy_intr.tgt_status_reg =
  1443. nx_legacy_intr->tgt_status_reg;
  1444. ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
  1445. ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
  1446. } else {
  1447. ha->isp_ops = &qla4xxx_isp_ops;
  1448. }
  1449. /* Set EEH reset type to fundamental if required by hba */
  1450. if (is_qla8022(ha))
  1451. pdev->needs_freset = 1;
  1452. /* Configure PCI I/O space. */
  1453. ret = ha->isp_ops->iospace_config(ha);
  1454. if (ret)
  1455. goto probe_failed_ioconfig;
  1456. ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
  1457. pdev->device, pdev->irq, ha->reg);
  1458. qla4xxx_config_dma_addressing(ha);
  1459. /* Initialize lists and spinlocks. */
  1460. INIT_LIST_HEAD(&ha->ddb_list);
  1461. INIT_LIST_HEAD(&ha->free_srb_q);
  1462. mutex_init(&ha->mbox_sem);
  1463. init_completion(&ha->mbx_intr_comp);
  1464. spin_lock_init(&ha->hardware_lock);
  1465. /* Allocate dma buffers */
  1466. if (qla4xxx_mem_alloc(ha)) {
  1467. ql4_printk(KERN_WARNING, ha,
  1468. "[ERROR] Failed to allocate memory for adapter\n");
  1469. ret = -ENOMEM;
  1470. goto probe_failed;
  1471. }
  1472. if (is_qla8022(ha))
  1473. (void) qla4_8xxx_get_flash_info(ha);
  1474. /*
  1475. * Initialize the Host adapter request/response queues and
  1476. * firmware
  1477. * NOTE: interrupts enabled upon successful completion
  1478. */
  1479. status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
  1480. while ((!test_bit(AF_ONLINE, &ha->flags)) &&
  1481. init_retry_count++ < MAX_INIT_RETRIES) {
  1482. if (is_qla8022(ha)) {
  1483. qla4_8xxx_idc_lock(ha);
  1484. dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
  1485. qla4_8xxx_idc_unlock(ha);
  1486. if (dev_state == QLA82XX_DEV_FAILED) {
  1487. ql4_printk(KERN_WARNING, ha, "%s: don't retry "
  1488. "initialize adapter. H/W is in failed state\n",
  1489. __func__);
  1490. break;
  1491. }
  1492. }
  1493. DEBUG2(printk("scsi: %s: retrying adapter initialization "
  1494. "(%d)\n", __func__, init_retry_count));
  1495. if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
  1496. continue;
  1497. status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
  1498. }
  1499. if (!test_bit(AF_ONLINE, &ha->flags)) {
  1500. ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
  1501. if (is_qla8022(ha) && ql4xdontresethba) {
  1502. /* Put the device in failed state. */
  1503. DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
  1504. qla4_8xxx_idc_lock(ha);
  1505. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  1506. QLA82XX_DEV_FAILED);
  1507. qla4_8xxx_idc_unlock(ha);
  1508. }
  1509. ret = -ENODEV;
  1510. goto probe_failed;
  1511. }
  1512. host->cmd_per_lun = 3;
  1513. host->max_channel = 0;
  1514. host->max_lun = MAX_LUNS - 1;
  1515. host->max_id = MAX_TARGETS;
  1516. host->max_cmd_len = IOCB_MAX_CDB_LEN;
  1517. host->can_queue = MAX_SRBS ;
  1518. host->transportt = qla4xxx_scsi_transport;
  1519. ret = scsi_init_shared_tag_map(host, MAX_SRBS);
  1520. if (ret) {
  1521. ql4_printk(KERN_WARNING, ha,
  1522. "scsi_init_shared_tag_map failed\n");
  1523. goto probe_failed;
  1524. }
  1525. /* Startup the kernel thread for this host adapter. */
  1526. DEBUG2(printk("scsi: %s: Starting kernel thread for "
  1527. "qla4xxx_dpc\n", __func__));
  1528. sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
  1529. ha->dpc_thread = create_singlethread_workqueue(buf);
  1530. if (!ha->dpc_thread) {
  1531. ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
  1532. ret = -ENODEV;
  1533. goto probe_failed;
  1534. }
  1535. INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
  1536. /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
  1537. * (which is called indirectly by qla4xxx_initialize_adapter),
  1538. * so that irqs will be registered after crbinit but before
  1539. * mbx_intr_enable.
  1540. */
  1541. if (!is_qla8022(ha)) {
  1542. ret = qla4xxx_request_irqs(ha);
  1543. if (ret) {
  1544. ql4_printk(KERN_WARNING, ha, "Failed to reserve "
  1545. "interrupt %d already in use.\n", pdev->irq);
  1546. goto probe_failed;
  1547. }
  1548. }
  1549. pci_save_state(ha->pdev);
  1550. ha->isp_ops->enable_intrs(ha);
  1551. /* Start timer thread. */
  1552. qla4xxx_start_timer(ha, qla4xxx_timer, 1);
  1553. set_bit(AF_INIT_DONE, &ha->flags);
  1554. pci_set_drvdata(pdev, ha);
  1555. ret = scsi_add_host(host, &pdev->dev);
  1556. if (ret)
  1557. goto probe_failed;
  1558. printk(KERN_INFO
  1559. " QLogic iSCSI HBA Driver version: %s\n"
  1560. " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
  1561. qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
  1562. ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
  1563. ha->patch_number, ha->build_number);
  1564. scsi_scan_host(host);
  1565. return 0;
  1566. probe_failed:
  1567. qla4xxx_free_adapter(ha);
  1568. probe_failed_ioconfig:
  1569. pci_disable_pcie_error_reporting(pdev);
  1570. scsi_host_put(ha->host);
  1571. probe_disable_device:
  1572. pci_disable_device(pdev);
  1573. return ret;
  1574. }
  1575. /**
  1576. * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
  1577. * @ha: pointer to adapter structure
  1578. *
  1579. * Mark the other ISP-4xxx port to indicate that the driver is being removed,
  1580. * so that the other port will not re-initialize while in the process of
  1581. * removing the ha due to driver unload or hba hotplug.
  1582. **/
  1583. static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
  1584. {
  1585. struct scsi_qla_host *other_ha = NULL;
  1586. struct pci_dev *other_pdev = NULL;
  1587. int fn = ISP4XXX_PCI_FN_2;
  1588. /*iscsi function numbers for ISP4xxx is 1 and 3*/
  1589. if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
  1590. fn = ISP4XXX_PCI_FN_1;
  1591. other_pdev =
  1592. pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
  1593. ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
  1594. fn));
  1595. /* Get other_ha if other_pdev is valid and state is enable*/
  1596. if (other_pdev) {
  1597. if (atomic_read(&other_pdev->enable_cnt)) {
  1598. other_ha = pci_get_drvdata(other_pdev);
  1599. if (other_ha) {
  1600. set_bit(AF_HA_REMOVAL, &other_ha->flags);
  1601. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
  1602. "Prevent %s reinit\n", __func__,
  1603. dev_name(&other_ha->pdev->dev)));
  1604. }
  1605. }
  1606. pci_dev_put(other_pdev);
  1607. }
  1608. }
  1609. /**
  1610. * qla4xxx_remove_adapter - calback function to remove adapter.
  1611. * @pci_dev: PCI device pointer
  1612. **/
  1613. static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
  1614. {
  1615. struct scsi_qla_host *ha;
  1616. ha = pci_get_drvdata(pdev);
  1617. if (!is_qla8022(ha))
  1618. qla4xxx_prevent_other_port_reinit(ha);
  1619. /* remove devs from iscsi_sessions to scsi_devices */
  1620. qla4xxx_free_ddb_list(ha);
  1621. scsi_remove_host(ha->host);
  1622. qla4xxx_free_adapter(ha);
  1623. scsi_host_put(ha->host);
  1624. pci_disable_pcie_error_reporting(pdev);
  1625. pci_disable_device(pdev);
  1626. pci_set_drvdata(pdev, NULL);
  1627. }
  1628. /**
  1629. * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
  1630. * @ha: HA context
  1631. *
  1632. * At exit, the @ha's flags.enable_64bit_addressing set to indicated
  1633. * supported addressing method.
  1634. */
  1635. static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
  1636. {
  1637. int retval;
  1638. /* Update our PCI device dma_mask for full 64 bit mask */
  1639. if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
  1640. if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
  1641. dev_dbg(&ha->pdev->dev,
  1642. "Failed to set 64 bit PCI consistent mask; "
  1643. "using 32 bit.\n");
  1644. retval = pci_set_consistent_dma_mask(ha->pdev,
  1645. DMA_BIT_MASK(32));
  1646. }
  1647. } else
  1648. retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
  1649. }
  1650. static int qla4xxx_slave_alloc(struct scsi_device *sdev)
  1651. {
  1652. struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
  1653. struct ddb_entry *ddb = sess->dd_data;
  1654. int queue_depth = QL4_DEF_QDEPTH;
  1655. sdev->hostdata = ddb;
  1656. sdev->tagged_supported = 1;
  1657. if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
  1658. queue_depth = ql4xmaxqdepth;
  1659. scsi_activate_tcq(sdev, queue_depth);
  1660. return 0;
  1661. }
  1662. static int qla4xxx_slave_configure(struct scsi_device *sdev)
  1663. {
  1664. sdev->tagged_supported = 1;
  1665. return 0;
  1666. }
  1667. static void qla4xxx_slave_destroy(struct scsi_device *sdev)
  1668. {
  1669. scsi_deactivate_tcq(sdev, 1);
  1670. }
  1671. /**
  1672. * qla4xxx_del_from_active_array - returns an active srb
  1673. * @ha: Pointer to host adapter structure.
  1674. * @index: index into the active_array
  1675. *
  1676. * This routine removes and returns the srb at the specified index
  1677. **/
  1678. struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
  1679. uint32_t index)
  1680. {
  1681. struct srb *srb = NULL;
  1682. struct scsi_cmnd *cmd = NULL;
  1683. cmd = scsi_host_find_tag(ha->host, index);
  1684. if (!cmd)
  1685. return srb;
  1686. srb = (struct srb *)CMD_SP(cmd);
  1687. if (!srb)
  1688. return srb;
  1689. /* update counters */
  1690. if (srb->flags & SRB_DMA_VALID) {
  1691. ha->req_q_count += srb->iocb_cnt;
  1692. ha->iocb_cnt -= srb->iocb_cnt;
  1693. if (srb->cmd)
  1694. srb->cmd->host_scribble =
  1695. (unsigned char *)(unsigned long) MAX_SRBS;
  1696. }
  1697. return srb;
  1698. }
  1699. /**
  1700. * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
  1701. * @ha: Pointer to host adapter structure.
  1702. * @cmd: Scsi Command to wait on.
  1703. *
  1704. * This routine waits for the command to be returned by the Firmware
  1705. * for some max time.
  1706. **/
  1707. static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
  1708. struct scsi_cmnd *cmd)
  1709. {
  1710. int done = 0;
  1711. struct srb *rp;
  1712. uint32_t max_wait_time = EH_WAIT_CMD_TOV;
  1713. int ret = SUCCESS;
  1714. /* Dont wait on command if PCI error is being handled
  1715. * by PCI AER driver
  1716. */
  1717. if (unlikely(pci_channel_offline(ha->pdev)) ||
  1718. (test_bit(AF_EEH_BUSY, &ha->flags))) {
  1719. ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
  1720. ha->host_no, __func__);
  1721. return ret;
  1722. }
  1723. do {
  1724. /* Checking to see if its returned to OS */
  1725. rp = (struct srb *) CMD_SP(cmd);
  1726. if (rp == NULL) {
  1727. done++;
  1728. break;
  1729. }
  1730. msleep(2000);
  1731. } while (max_wait_time--);
  1732. return done;
  1733. }
  1734. /**
  1735. * qla4xxx_wait_for_hba_online - waits for HBA to come online
  1736. * @ha: Pointer to host adapter structure
  1737. **/
  1738. static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
  1739. {
  1740. unsigned long wait_online;
  1741. wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
  1742. while (time_before(jiffies, wait_online)) {
  1743. if (adapter_up(ha))
  1744. return QLA_SUCCESS;
  1745. msleep(2000);
  1746. }
  1747. return QLA_ERROR;
  1748. }
  1749. /**
  1750. * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
  1751. * @ha: pointer to HBA
  1752. * @t: target id
  1753. * @l: lun id
  1754. *
  1755. * This function waits for all outstanding commands to a lun to complete. It
  1756. * returns 0 if all pending commands are returned and 1 otherwise.
  1757. **/
  1758. static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
  1759. struct scsi_target *stgt,
  1760. struct scsi_device *sdev)
  1761. {
  1762. int cnt;
  1763. int status = 0;
  1764. struct scsi_cmnd *cmd;
  1765. /*
  1766. * Waiting for all commands for the designated target or dev
  1767. * in the active array
  1768. */
  1769. for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
  1770. cmd = scsi_host_find_tag(ha->host, cnt);
  1771. if (cmd && stgt == scsi_target(cmd->device) &&
  1772. (!sdev || sdev == cmd->device)) {
  1773. if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
  1774. status++;
  1775. break;
  1776. }
  1777. }
  1778. }
  1779. return status;
  1780. }
  1781. /**
  1782. * qla4xxx_eh_abort - callback for abort task.
  1783. * @cmd: Pointer to Linux's SCSI command structure
  1784. *
  1785. * This routine is called by the Linux OS to abort the specified
  1786. * command.
  1787. **/
  1788. static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
  1789. {
  1790. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  1791. unsigned int id = cmd->device->id;
  1792. unsigned int lun = cmd->device->lun;
  1793. unsigned long flags;
  1794. struct srb *srb = NULL;
  1795. int ret = SUCCESS;
  1796. int wait = 0;
  1797. ql4_printk(KERN_INFO, ha,
  1798. "scsi%ld:%d:%d: Abort command issued cmd=%p\n",
  1799. ha->host_no, id, lun, cmd);
  1800. spin_lock_irqsave(&ha->hardware_lock, flags);
  1801. srb = (struct srb *) CMD_SP(cmd);
  1802. if (!srb) {
  1803. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1804. return SUCCESS;
  1805. }
  1806. kref_get(&srb->srb_ref);
  1807. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1808. if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
  1809. DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
  1810. ha->host_no, id, lun));
  1811. ret = FAILED;
  1812. } else {
  1813. DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
  1814. ha->host_no, id, lun));
  1815. wait = 1;
  1816. }
  1817. kref_put(&srb->srb_ref, qla4xxx_srb_compl);
  1818. /* Wait for command to complete */
  1819. if (wait) {
  1820. if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
  1821. DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
  1822. ha->host_no, id, lun));
  1823. ret = FAILED;
  1824. }
  1825. }
  1826. ql4_printk(KERN_INFO, ha,
  1827. "scsi%ld:%d:%d: Abort command - %s\n",
  1828. ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
  1829. return ret;
  1830. }
  1831. /**
  1832. * qla4xxx_eh_device_reset - callback for target reset.
  1833. * @cmd: Pointer to Linux's SCSI command structure
  1834. *
  1835. * This routine is called by the Linux OS to reset all luns on the
  1836. * specified target.
  1837. **/
  1838. static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
  1839. {
  1840. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  1841. struct ddb_entry *ddb_entry = cmd->device->hostdata;
  1842. int ret = FAILED, stat;
  1843. if (!ddb_entry)
  1844. return ret;
  1845. ret = iscsi_block_scsi_eh(cmd);
  1846. if (ret)
  1847. return ret;
  1848. ret = FAILED;
  1849. ql4_printk(KERN_INFO, ha,
  1850. "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
  1851. cmd->device->channel, cmd->device->id, cmd->device->lun);
  1852. DEBUG2(printk(KERN_INFO
  1853. "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
  1854. "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
  1855. cmd, jiffies, cmd->request->timeout / HZ,
  1856. ha->dpc_flags, cmd->result, cmd->allowed));
  1857. /* FIXME: wait for hba to go online */
  1858. stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
  1859. if (stat != QLA_SUCCESS) {
  1860. ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
  1861. goto eh_dev_reset_done;
  1862. }
  1863. if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
  1864. cmd->device)) {
  1865. ql4_printk(KERN_INFO, ha,
  1866. "DEVICE RESET FAILED - waiting for "
  1867. "commands.\n");
  1868. goto eh_dev_reset_done;
  1869. }
  1870. /* Send marker. */
  1871. if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
  1872. MM_LUN_RESET) != QLA_SUCCESS)
  1873. goto eh_dev_reset_done;
  1874. ql4_printk(KERN_INFO, ha,
  1875. "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
  1876. ha->host_no, cmd->device->channel, cmd->device->id,
  1877. cmd->device->lun);
  1878. ret = SUCCESS;
  1879. eh_dev_reset_done:
  1880. return ret;
  1881. }
  1882. /**
  1883. * qla4xxx_eh_target_reset - callback for target reset.
  1884. * @cmd: Pointer to Linux's SCSI command structure
  1885. *
  1886. * This routine is called by the Linux OS to reset the target.
  1887. **/
  1888. static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
  1889. {
  1890. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  1891. struct ddb_entry *ddb_entry = cmd->device->hostdata;
  1892. int stat, ret;
  1893. if (!ddb_entry)
  1894. return FAILED;
  1895. ret = iscsi_block_scsi_eh(cmd);
  1896. if (ret)
  1897. return ret;
  1898. starget_printk(KERN_INFO, scsi_target(cmd->device),
  1899. "WARM TARGET RESET ISSUED.\n");
  1900. DEBUG2(printk(KERN_INFO
  1901. "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
  1902. "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
  1903. ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
  1904. ha->dpc_flags, cmd->result, cmd->allowed));
  1905. stat = qla4xxx_reset_target(ha, ddb_entry);
  1906. if (stat != QLA_SUCCESS) {
  1907. starget_printk(KERN_INFO, scsi_target(cmd->device),
  1908. "WARM TARGET RESET FAILED.\n");
  1909. return FAILED;
  1910. }
  1911. if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
  1912. NULL)) {
  1913. starget_printk(KERN_INFO, scsi_target(cmd->device),
  1914. "WARM TARGET DEVICE RESET FAILED - "
  1915. "waiting for commands.\n");
  1916. return FAILED;
  1917. }
  1918. /* Send marker. */
  1919. if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
  1920. MM_TGT_WARM_RESET) != QLA_SUCCESS) {
  1921. starget_printk(KERN_INFO, scsi_target(cmd->device),
  1922. "WARM TARGET DEVICE RESET FAILED - "
  1923. "marker iocb failed.\n");
  1924. return FAILED;
  1925. }
  1926. starget_printk(KERN_INFO, scsi_target(cmd->device),
  1927. "WARM TARGET RESET SUCCEEDED.\n");
  1928. return SUCCESS;
  1929. }
  1930. /**
  1931. * qla4xxx_eh_host_reset - kernel callback
  1932. * @cmd: Pointer to Linux's SCSI command structure
  1933. *
  1934. * This routine is invoked by the Linux kernel to perform fatal error
  1935. * recovery on the specified adapter.
  1936. **/
  1937. static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
  1938. {
  1939. int return_status = FAILED;
  1940. struct scsi_qla_host *ha;
  1941. ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
  1942. if (ql4xdontresethba) {
  1943. DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
  1944. ha->host_no, __func__));
  1945. return FAILED;
  1946. }
  1947. ql4_printk(KERN_INFO, ha,
  1948. "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
  1949. cmd->device->channel, cmd->device->id, cmd->device->lun);
  1950. if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
  1951. DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
  1952. "DEAD.\n", ha->host_no, cmd->device->channel,
  1953. __func__));
  1954. return FAILED;
  1955. }
  1956. if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
  1957. if (is_qla8022(ha))
  1958. set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  1959. else
  1960. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  1961. }
  1962. if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
  1963. return_status = SUCCESS;
  1964. ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
  1965. return_status == FAILED ? "FAILED" : "SUCCEEDED");
  1966. return return_status;
  1967. }
  1968. /* PCI AER driver recovers from all correctable errors w/o
  1969. * driver intervention. For uncorrectable errors PCI AER
  1970. * driver calls the following device driver's callbacks
  1971. *
  1972. * - Fatal Errors - link_reset
  1973. * - Non-Fatal Errors - driver's pci_error_detected() which
  1974. * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
  1975. *
  1976. * PCI AER driver calls
  1977. * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
  1978. * returns RECOVERED or NEED_RESET if fw_hung
  1979. * NEED_RESET - driver's slot_reset()
  1980. * DISCONNECT - device is dead & cannot recover
  1981. * RECOVERED - driver's pci_resume()
  1982. */
  1983. static pci_ers_result_t
  1984. qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
  1985. {
  1986. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  1987. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
  1988. ha->host_no, __func__, state);
  1989. if (!is_aer_supported(ha))
  1990. return PCI_ERS_RESULT_NONE;
  1991. switch (state) {
  1992. case pci_channel_io_normal:
  1993. clear_bit(AF_EEH_BUSY, &ha->flags);
  1994. return PCI_ERS_RESULT_CAN_RECOVER;
  1995. case pci_channel_io_frozen:
  1996. set_bit(AF_EEH_BUSY, &ha->flags);
  1997. qla4xxx_mailbox_premature_completion(ha);
  1998. qla4xxx_free_irqs(ha);
  1999. pci_disable_device(pdev);
  2000. /* Return back all IOs */
  2001. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  2002. return PCI_ERS_RESULT_NEED_RESET;
  2003. case pci_channel_io_perm_failure:
  2004. set_bit(AF_EEH_BUSY, &ha->flags);
  2005. set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
  2006. qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
  2007. return PCI_ERS_RESULT_DISCONNECT;
  2008. }
  2009. return PCI_ERS_RESULT_NEED_RESET;
  2010. }
  2011. /**
  2012. * qla4xxx_pci_mmio_enabled() gets called if
  2013. * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
  2014. * and read/write to the device still works.
  2015. **/
  2016. static pci_ers_result_t
  2017. qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
  2018. {
  2019. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  2020. if (!is_aer_supported(ha))
  2021. return PCI_ERS_RESULT_NONE;
  2022. return PCI_ERS_RESULT_RECOVERED;
  2023. }
  2024. static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
  2025. {
  2026. uint32_t rval = QLA_ERROR;
  2027. uint32_t ret = 0;
  2028. int fn;
  2029. struct pci_dev *other_pdev = NULL;
  2030. ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
  2031. set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  2032. if (test_bit(AF_ONLINE, &ha->flags)) {
  2033. clear_bit(AF_ONLINE, &ha->flags);
  2034. qla4xxx_mark_all_devices_missing(ha);
  2035. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  2036. }
  2037. fn = PCI_FUNC(ha->pdev->devfn);
  2038. while (fn > 0) {
  2039. fn--;
  2040. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
  2041. "func %x\n", ha->host_no, __func__, fn);
  2042. /* Get the pci device given the domain, bus,
  2043. * slot/function number */
  2044. other_pdev =
  2045. pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
  2046. ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
  2047. fn));
  2048. if (!other_pdev)
  2049. continue;
  2050. if (atomic_read(&other_pdev->enable_cnt)) {
  2051. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
  2052. "func in enabled state%x\n", ha->host_no,
  2053. __func__, fn);
  2054. pci_dev_put(other_pdev);
  2055. break;
  2056. }
  2057. pci_dev_put(other_pdev);
  2058. }
  2059. /* The first function on the card, the reset owner will
  2060. * start & initialize the firmware. The other functions
  2061. * on the card will reset the firmware context
  2062. */
  2063. if (!fn) {
  2064. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
  2065. "0x%x is the owner\n", ha->host_no, __func__,
  2066. ha->pdev->devfn);
  2067. qla4_8xxx_idc_lock(ha);
  2068. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  2069. QLA82XX_DEV_COLD);
  2070. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
  2071. QLA82XX_IDC_VERSION);
  2072. qla4_8xxx_idc_unlock(ha);
  2073. clear_bit(AF_FW_RECOVERY, &ha->flags);
  2074. rval = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
  2075. qla4_8xxx_idc_lock(ha);
  2076. if (rval != QLA_SUCCESS) {
  2077. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
  2078. "FAILED\n", ha->host_no, __func__);
  2079. qla4_8xxx_clear_drv_active(ha);
  2080. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  2081. QLA82XX_DEV_FAILED);
  2082. } else {
  2083. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
  2084. "READY\n", ha->host_no, __func__);
  2085. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  2086. QLA82XX_DEV_READY);
  2087. /* Clear driver state register */
  2088. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
  2089. qla4_8xxx_set_drv_active(ha);
  2090. ret = qla4xxx_request_irqs(ha);
  2091. if (ret) {
  2092. ql4_printk(KERN_WARNING, ha, "Failed to "
  2093. "reserve interrupt %d already in use.\n",
  2094. ha->pdev->irq);
  2095. rval = QLA_ERROR;
  2096. } else {
  2097. ha->isp_ops->enable_intrs(ha);
  2098. rval = QLA_SUCCESS;
  2099. }
  2100. }
  2101. qla4_8xxx_idc_unlock(ha);
  2102. } else {
  2103. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
  2104. "the reset owner\n", ha->host_no, __func__,
  2105. ha->pdev->devfn);
  2106. if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
  2107. QLA82XX_DEV_READY)) {
  2108. clear_bit(AF_FW_RECOVERY, &ha->flags);
  2109. rval = qla4xxx_initialize_adapter(ha,
  2110. PRESERVE_DDB_LIST);
  2111. if (rval == QLA_SUCCESS) {
  2112. ret = qla4xxx_request_irqs(ha);
  2113. if (ret) {
  2114. ql4_printk(KERN_WARNING, ha, "Failed to"
  2115. " reserve interrupt %d already in"
  2116. " use.\n", ha->pdev->irq);
  2117. rval = QLA_ERROR;
  2118. } else {
  2119. ha->isp_ops->enable_intrs(ha);
  2120. rval = QLA_SUCCESS;
  2121. }
  2122. }
  2123. qla4_8xxx_idc_lock(ha);
  2124. qla4_8xxx_set_drv_active(ha);
  2125. qla4_8xxx_idc_unlock(ha);
  2126. }
  2127. }
  2128. clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  2129. return rval;
  2130. }
  2131. static pci_ers_result_t
  2132. qla4xxx_pci_slot_reset(struct pci_dev *pdev)
  2133. {
  2134. pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
  2135. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  2136. int rc;
  2137. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
  2138. ha->host_no, __func__);
  2139. if (!is_aer_supported(ha))
  2140. return PCI_ERS_RESULT_NONE;
  2141. /* Restore the saved state of PCIe device -
  2142. * BAR registers, PCI Config space, PCIX, MSI,
  2143. * IOV states
  2144. */
  2145. pci_restore_state(pdev);
  2146. /* pci_restore_state() clears the saved_state flag of the device
  2147. * save restored state which resets saved_state flag
  2148. */
  2149. pci_save_state(pdev);
  2150. /* Initialize device or resume if in suspended state */
  2151. rc = pci_enable_device(pdev);
  2152. if (rc) {
  2153. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
  2154. "device after reset\n", ha->host_no, __func__);
  2155. goto exit_slot_reset;
  2156. }
  2157. ha->isp_ops->disable_intrs(ha);
  2158. if (is_qla8022(ha)) {
  2159. if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
  2160. ret = PCI_ERS_RESULT_RECOVERED;
  2161. goto exit_slot_reset;
  2162. } else
  2163. goto exit_slot_reset;
  2164. }
  2165. exit_slot_reset:
  2166. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
  2167. "device after reset\n", ha->host_no, __func__, ret);
  2168. return ret;
  2169. }
  2170. static void
  2171. qla4xxx_pci_resume(struct pci_dev *pdev)
  2172. {
  2173. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  2174. int ret;
  2175. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
  2176. ha->host_no, __func__);
  2177. ret = qla4xxx_wait_for_hba_online(ha);
  2178. if (ret != QLA_SUCCESS) {
  2179. ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
  2180. "resume I/O from slot/link_reset\n", ha->host_no,
  2181. __func__);
  2182. }
  2183. pci_cleanup_aer_uncorrect_error_status(pdev);
  2184. clear_bit(AF_EEH_BUSY, &ha->flags);
  2185. }
  2186. static struct pci_error_handlers qla4xxx_err_handler = {
  2187. .error_detected = qla4xxx_pci_error_detected,
  2188. .mmio_enabled = qla4xxx_pci_mmio_enabled,
  2189. .slot_reset = qla4xxx_pci_slot_reset,
  2190. .resume = qla4xxx_pci_resume,
  2191. };
  2192. static struct pci_device_id qla4xxx_pci_tbl[] = {
  2193. {
  2194. .vendor = PCI_VENDOR_ID_QLOGIC,
  2195. .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
  2196. .subvendor = PCI_ANY_ID,
  2197. .subdevice = PCI_ANY_ID,
  2198. },
  2199. {
  2200. .vendor = PCI_VENDOR_ID_QLOGIC,
  2201. .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
  2202. .subvendor = PCI_ANY_ID,
  2203. .subdevice = PCI_ANY_ID,
  2204. },
  2205. {
  2206. .vendor = PCI_VENDOR_ID_QLOGIC,
  2207. .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
  2208. .subvendor = PCI_ANY_ID,
  2209. .subdevice = PCI_ANY_ID,
  2210. },
  2211. {
  2212. .vendor = PCI_VENDOR_ID_QLOGIC,
  2213. .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
  2214. .subvendor = PCI_ANY_ID,
  2215. .subdevice = PCI_ANY_ID,
  2216. },
  2217. {0, 0},
  2218. };
  2219. MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
  2220. static struct pci_driver qla4xxx_pci_driver = {
  2221. .name = DRIVER_NAME,
  2222. .id_table = qla4xxx_pci_tbl,
  2223. .probe = qla4xxx_probe_adapter,
  2224. .remove = qla4xxx_remove_adapter,
  2225. .err_handler = &qla4xxx_err_handler,
  2226. };
  2227. static int __init qla4xxx_module_init(void)
  2228. {
  2229. int ret;
  2230. /* Allocate cache for SRBs. */
  2231. srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
  2232. SLAB_HWCACHE_ALIGN, NULL);
  2233. if (srb_cachep == NULL) {
  2234. printk(KERN_ERR
  2235. "%s: Unable to allocate SRB cache..."
  2236. "Failing load!\n", DRIVER_NAME);
  2237. ret = -ENOMEM;
  2238. goto no_srp_cache;
  2239. }
  2240. /* Derive version string. */
  2241. strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
  2242. if (ql4xextended_error_logging)
  2243. strcat(qla4xxx_version_str, "-debug");
  2244. qla4xxx_scsi_transport =
  2245. iscsi_register_transport(&qla4xxx_iscsi_transport);
  2246. if (!qla4xxx_scsi_transport){
  2247. ret = -ENODEV;
  2248. goto release_srb_cache;
  2249. }
  2250. ret = pci_register_driver(&qla4xxx_pci_driver);
  2251. if (ret)
  2252. goto unregister_transport;
  2253. printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
  2254. return 0;
  2255. unregister_transport:
  2256. iscsi_unregister_transport(&qla4xxx_iscsi_transport);
  2257. release_srb_cache:
  2258. kmem_cache_destroy(srb_cachep);
  2259. no_srp_cache:
  2260. return ret;
  2261. }
  2262. static void __exit qla4xxx_module_exit(void)
  2263. {
  2264. pci_unregister_driver(&qla4xxx_pci_driver);
  2265. iscsi_unregister_transport(&qla4xxx_iscsi_transport);
  2266. kmem_cache_destroy(srb_cachep);
  2267. }
  2268. module_init(qla4xxx_module_init);
  2269. module_exit(qla4xxx_module_exit);
  2270. MODULE_AUTHOR("QLogic Corporation");
  2271. MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
  2272. MODULE_LICENSE("GPL");
  2273. MODULE_VERSION(QLA4XXX_DRIVER_VERSION);