qla_bsg.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024
  1. /*
  2. * QLogic Fibre Channel HBA Driver
  3. * Copyright (c) 2003-2012 QLogic Corporation
  4. *
  5. * See LICENSE.qla2xxx for copyright and licensing details.
  6. */
  7. #include "qla_def.h"
  8. #include <linux/kthread.h>
  9. #include <linux/vmalloc.h>
  10. #include <linux/delay.h>
  11. /* BSG support for ELS/CT pass through */
  12. void
  13. qla2x00_bsg_job_done(void *data, void *ptr, int res)
  14. {
  15. srb_t *sp = (srb_t *)ptr;
  16. struct scsi_qla_host *vha = (scsi_qla_host_t *)data;
  17. struct fc_bsg_job *bsg_job = sp->u.bsg_job;
  18. bsg_job->reply->result = res;
  19. bsg_job->job_done(bsg_job);
  20. sp->free(vha, sp);
  21. }
  22. void
  23. qla2x00_bsg_sp_free(void *data, void *ptr)
  24. {
  25. srb_t *sp = (srb_t *)ptr;
  26. struct scsi_qla_host *vha = sp->fcport->vha;
  27. struct fc_bsg_job *bsg_job = sp->u.bsg_job;
  28. struct qla_hw_data *ha = vha->hw;
  29. dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  30. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  31. dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
  32. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  33. if (sp->type == SRB_CT_CMD ||
  34. sp->type == SRB_ELS_CMD_HST)
  35. kfree(sp->fcport);
  36. qla2x00_rel_sp(vha, sp);
  37. }
  38. int
  39. qla24xx_fcp_prio_cfg_valid(scsi_qla_host_t *vha,
  40. struct qla_fcp_prio_cfg *pri_cfg, uint8_t flag)
  41. {
  42. int i, ret, num_valid;
  43. uint8_t *bcode;
  44. struct qla_fcp_prio_entry *pri_entry;
  45. uint32_t *bcode_val_ptr, bcode_val;
  46. ret = 1;
  47. num_valid = 0;
  48. bcode = (uint8_t *)pri_cfg;
  49. bcode_val_ptr = (uint32_t *)pri_cfg;
  50. bcode_val = (uint32_t)(*bcode_val_ptr);
  51. if (bcode_val == 0xFFFFFFFF) {
  52. /* No FCP Priority config data in flash */
  53. ql_dbg(ql_dbg_user, vha, 0x7051,
  54. "No FCP Priority config data.\n");
  55. return 0;
  56. }
  57. if (bcode[0] != 'H' || bcode[1] != 'Q' || bcode[2] != 'O' ||
  58. bcode[3] != 'S') {
  59. /* Invalid FCP priority data header*/
  60. ql_dbg(ql_dbg_user, vha, 0x7052,
  61. "Invalid FCP Priority data header. bcode=0x%x.\n",
  62. bcode_val);
  63. return 0;
  64. }
  65. if (flag != 1)
  66. return ret;
  67. pri_entry = &pri_cfg->entry[0];
  68. for (i = 0; i < pri_cfg->num_entries; i++) {
  69. if (pri_entry->flags & FCP_PRIO_ENTRY_TAG_VALID)
  70. num_valid++;
  71. pri_entry++;
  72. }
  73. if (num_valid == 0) {
  74. /* No valid FCP priority data entries */
  75. ql_dbg(ql_dbg_user, vha, 0x7053,
  76. "No valid FCP Priority data entries.\n");
  77. ret = 0;
  78. } else {
  79. /* FCP priority data is valid */
  80. ql_dbg(ql_dbg_user, vha, 0x7054,
  81. "Valid FCP priority data. num entries = %d.\n",
  82. num_valid);
  83. }
  84. return ret;
  85. }
  86. static int
  87. qla24xx_proc_fcp_prio_cfg_cmd(struct fc_bsg_job *bsg_job)
  88. {
  89. struct Scsi_Host *host = bsg_job->shost;
  90. scsi_qla_host_t *vha = shost_priv(host);
  91. struct qla_hw_data *ha = vha->hw;
  92. int ret = 0;
  93. uint32_t len;
  94. uint32_t oper;
  95. if (!(IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha) || IS_QLA82XX(ha))) {
  96. ret = -EINVAL;
  97. goto exit_fcp_prio_cfg;
  98. }
  99. /* Get the sub command */
  100. oper = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
  101. /* Only set config is allowed if config memory is not allocated */
  102. if (!ha->fcp_prio_cfg && (oper != QLFC_FCP_PRIO_SET_CONFIG)) {
  103. ret = -EINVAL;
  104. goto exit_fcp_prio_cfg;
  105. }
  106. switch (oper) {
  107. case QLFC_FCP_PRIO_DISABLE:
  108. if (ha->flags.fcp_prio_enabled) {
  109. ha->flags.fcp_prio_enabled = 0;
  110. ha->fcp_prio_cfg->attributes &=
  111. ~FCP_PRIO_ATTR_ENABLE;
  112. qla24xx_update_all_fcp_prio(vha);
  113. bsg_job->reply->result = DID_OK;
  114. } else {
  115. ret = -EINVAL;
  116. bsg_job->reply->result = (DID_ERROR << 16);
  117. goto exit_fcp_prio_cfg;
  118. }
  119. break;
  120. case QLFC_FCP_PRIO_ENABLE:
  121. if (!ha->flags.fcp_prio_enabled) {
  122. if (ha->fcp_prio_cfg) {
  123. ha->flags.fcp_prio_enabled = 1;
  124. ha->fcp_prio_cfg->attributes |=
  125. FCP_PRIO_ATTR_ENABLE;
  126. qla24xx_update_all_fcp_prio(vha);
  127. bsg_job->reply->result = DID_OK;
  128. } else {
  129. ret = -EINVAL;
  130. bsg_job->reply->result = (DID_ERROR << 16);
  131. goto exit_fcp_prio_cfg;
  132. }
  133. }
  134. break;
  135. case QLFC_FCP_PRIO_GET_CONFIG:
  136. len = bsg_job->reply_payload.payload_len;
  137. if (!len || len > FCP_PRIO_CFG_SIZE) {
  138. ret = -EINVAL;
  139. bsg_job->reply->result = (DID_ERROR << 16);
  140. goto exit_fcp_prio_cfg;
  141. }
  142. bsg_job->reply->result = DID_OK;
  143. bsg_job->reply->reply_payload_rcv_len =
  144. sg_copy_from_buffer(
  145. bsg_job->reply_payload.sg_list,
  146. bsg_job->reply_payload.sg_cnt, ha->fcp_prio_cfg,
  147. len);
  148. break;
  149. case QLFC_FCP_PRIO_SET_CONFIG:
  150. len = bsg_job->request_payload.payload_len;
  151. if (!len || len > FCP_PRIO_CFG_SIZE) {
  152. bsg_job->reply->result = (DID_ERROR << 16);
  153. ret = -EINVAL;
  154. goto exit_fcp_prio_cfg;
  155. }
  156. if (!ha->fcp_prio_cfg) {
  157. ha->fcp_prio_cfg = vmalloc(FCP_PRIO_CFG_SIZE);
  158. if (!ha->fcp_prio_cfg) {
  159. ql_log(ql_log_warn, vha, 0x7050,
  160. "Unable to allocate memory for fcp prio "
  161. "config data (%x).\n", FCP_PRIO_CFG_SIZE);
  162. bsg_job->reply->result = (DID_ERROR << 16);
  163. ret = -ENOMEM;
  164. goto exit_fcp_prio_cfg;
  165. }
  166. }
  167. memset(ha->fcp_prio_cfg, 0, FCP_PRIO_CFG_SIZE);
  168. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  169. bsg_job->request_payload.sg_cnt, ha->fcp_prio_cfg,
  170. FCP_PRIO_CFG_SIZE);
  171. /* validate fcp priority data */
  172. if (!qla24xx_fcp_prio_cfg_valid(vha,
  173. (struct qla_fcp_prio_cfg *) ha->fcp_prio_cfg, 1)) {
  174. bsg_job->reply->result = (DID_ERROR << 16);
  175. ret = -EINVAL;
  176. /* If buffer was invalidatic int
  177. * fcp_prio_cfg is of no use
  178. */
  179. vfree(ha->fcp_prio_cfg);
  180. ha->fcp_prio_cfg = NULL;
  181. goto exit_fcp_prio_cfg;
  182. }
  183. ha->flags.fcp_prio_enabled = 0;
  184. if (ha->fcp_prio_cfg->attributes & FCP_PRIO_ATTR_ENABLE)
  185. ha->flags.fcp_prio_enabled = 1;
  186. qla24xx_update_all_fcp_prio(vha);
  187. bsg_job->reply->result = DID_OK;
  188. break;
  189. default:
  190. ret = -EINVAL;
  191. break;
  192. }
  193. exit_fcp_prio_cfg:
  194. if (!ret)
  195. bsg_job->job_done(bsg_job);
  196. return ret;
  197. }
  198. static int
  199. qla2x00_process_els(struct fc_bsg_job *bsg_job)
  200. {
  201. struct fc_rport *rport;
  202. fc_port_t *fcport = NULL;
  203. struct Scsi_Host *host;
  204. scsi_qla_host_t *vha;
  205. struct qla_hw_data *ha;
  206. srb_t *sp;
  207. const char *type;
  208. int req_sg_cnt, rsp_sg_cnt;
  209. int rval = (DRIVER_ERROR << 16);
  210. uint16_t nextlid = 0;
  211. if (bsg_job->request->msgcode == FC_BSG_RPT_ELS) {
  212. rport = bsg_job->rport;
  213. fcport = *(fc_port_t **) rport->dd_data;
  214. host = rport_to_shost(rport);
  215. vha = shost_priv(host);
  216. ha = vha->hw;
  217. type = "FC_BSG_RPT_ELS";
  218. } else {
  219. host = bsg_job->shost;
  220. vha = shost_priv(host);
  221. ha = vha->hw;
  222. type = "FC_BSG_HST_ELS_NOLOGIN";
  223. }
  224. /* pass through is supported only for ISP 4Gb or higher */
  225. if (!IS_FWI2_CAPABLE(ha)) {
  226. ql_dbg(ql_dbg_user, vha, 0x7001,
  227. "ELS passthru not supported for ISP23xx based adapters.\n");
  228. rval = -EPERM;
  229. goto done;
  230. }
  231. /* Multiple SG's are not supported for ELS requests */
  232. if (bsg_job->request_payload.sg_cnt > 1 ||
  233. bsg_job->reply_payload.sg_cnt > 1) {
  234. ql_dbg(ql_dbg_user, vha, 0x7002,
  235. "Multiple SG's are not suppored for ELS requests, "
  236. "request_sg_cnt=%x reply_sg_cnt=%x.\n",
  237. bsg_job->request_payload.sg_cnt,
  238. bsg_job->reply_payload.sg_cnt);
  239. rval = -EPERM;
  240. goto done;
  241. }
  242. /* ELS request for rport */
  243. if (bsg_job->request->msgcode == FC_BSG_RPT_ELS) {
  244. /* make sure the rport is logged in,
  245. * if not perform fabric login
  246. */
  247. if (qla2x00_fabric_login(vha, fcport, &nextlid)) {
  248. ql_dbg(ql_dbg_user, vha, 0x7003,
  249. "Failed to login port %06X for ELS passthru.\n",
  250. fcport->d_id.b24);
  251. rval = -EIO;
  252. goto done;
  253. }
  254. } else {
  255. /* Allocate a dummy fcport structure, since functions
  256. * preparing the IOCB and mailbox command retrieves port
  257. * specific information from fcport structure. For Host based
  258. * ELS commands there will be no fcport structure allocated
  259. */
  260. fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
  261. if (!fcport) {
  262. rval = -ENOMEM;
  263. goto done;
  264. }
  265. /* Initialize all required fields of fcport */
  266. fcport->vha = vha;
  267. fcport->d_id.b.al_pa =
  268. bsg_job->request->rqst_data.h_els.port_id[0];
  269. fcport->d_id.b.area =
  270. bsg_job->request->rqst_data.h_els.port_id[1];
  271. fcport->d_id.b.domain =
  272. bsg_job->request->rqst_data.h_els.port_id[2];
  273. fcport->loop_id =
  274. (fcport->d_id.b.al_pa == 0xFD) ?
  275. NPH_FABRIC_CONTROLLER : NPH_F_PORT;
  276. }
  277. if (!vha->flags.online) {
  278. ql_log(ql_log_warn, vha, 0x7005, "Host not online.\n");
  279. rval = -EIO;
  280. goto done;
  281. }
  282. req_sg_cnt =
  283. dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  284. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  285. if (!req_sg_cnt) {
  286. rval = -ENOMEM;
  287. goto done_free_fcport;
  288. }
  289. rsp_sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
  290. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  291. if (!rsp_sg_cnt) {
  292. rval = -ENOMEM;
  293. goto done_free_fcport;
  294. }
  295. if ((req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
  296. (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
  297. ql_log(ql_log_warn, vha, 0x7008,
  298. "dma mapping resulted in different sg counts, "
  299. "request_sg_cnt: %x dma_request_sg_cnt:%x reply_sg_cnt:%x "
  300. "dma_reply_sg_cnt:%x.\n", bsg_job->request_payload.sg_cnt,
  301. req_sg_cnt, bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
  302. rval = -EAGAIN;
  303. goto done_unmap_sg;
  304. }
  305. /* Alloc SRB structure */
  306. sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
  307. if (!sp) {
  308. rval = -ENOMEM;
  309. goto done_unmap_sg;
  310. }
  311. sp->type =
  312. (bsg_job->request->msgcode == FC_BSG_RPT_ELS ?
  313. SRB_ELS_CMD_RPT : SRB_ELS_CMD_HST);
  314. sp->name =
  315. (bsg_job->request->msgcode == FC_BSG_RPT_ELS ?
  316. "bsg_els_rpt" : "bsg_els_hst");
  317. sp->u.bsg_job = bsg_job;
  318. sp->free = qla2x00_bsg_sp_free;
  319. sp->done = qla2x00_bsg_job_done;
  320. ql_dbg(ql_dbg_user, vha, 0x700a,
  321. "bsg rqst type: %s els type: %x - loop-id=%x "
  322. "portid=%-2x%02x%02x.\n", type,
  323. bsg_job->request->rqst_data.h_els.command_code, fcport->loop_id,
  324. fcport->d_id.b.domain, fcport->d_id.b.area, fcport->d_id.b.al_pa);
  325. rval = qla2x00_start_sp(sp);
  326. if (rval != QLA_SUCCESS) {
  327. ql_log(ql_log_warn, vha, 0x700e,
  328. "qla2x00_start_sp failed = %d\n", rval);
  329. qla2x00_rel_sp(vha, sp);
  330. rval = -EIO;
  331. goto done_unmap_sg;
  332. }
  333. return rval;
  334. done_unmap_sg:
  335. dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  336. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  337. dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
  338. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  339. goto done_free_fcport;
  340. done_free_fcport:
  341. if (bsg_job->request->msgcode == FC_BSG_HST_ELS_NOLOGIN)
  342. kfree(fcport);
  343. done:
  344. return rval;
  345. }
  346. inline uint16_t
  347. qla24xx_calc_ct_iocbs(uint16_t dsds)
  348. {
  349. uint16_t iocbs;
  350. iocbs = 1;
  351. if (dsds > 2) {
  352. iocbs += (dsds - 2) / 5;
  353. if ((dsds - 2) % 5)
  354. iocbs++;
  355. }
  356. return iocbs;
  357. }
  358. static int
  359. qla2x00_process_ct(struct fc_bsg_job *bsg_job)
  360. {
  361. srb_t *sp;
  362. struct Scsi_Host *host = bsg_job->shost;
  363. scsi_qla_host_t *vha = shost_priv(host);
  364. struct qla_hw_data *ha = vha->hw;
  365. int rval = (DRIVER_ERROR << 16);
  366. int req_sg_cnt, rsp_sg_cnt;
  367. uint16_t loop_id;
  368. struct fc_port *fcport;
  369. char *type = "FC_BSG_HST_CT";
  370. req_sg_cnt =
  371. dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  372. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  373. if (!req_sg_cnt) {
  374. ql_log(ql_log_warn, vha, 0x700f,
  375. "dma_map_sg return %d for request\n", req_sg_cnt);
  376. rval = -ENOMEM;
  377. goto done;
  378. }
  379. rsp_sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
  380. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  381. if (!rsp_sg_cnt) {
  382. ql_log(ql_log_warn, vha, 0x7010,
  383. "dma_map_sg return %d for reply\n", rsp_sg_cnt);
  384. rval = -ENOMEM;
  385. goto done;
  386. }
  387. if ((req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
  388. (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
  389. ql_log(ql_log_warn, vha, 0x7011,
  390. "request_sg_cnt: %x dma_request_sg_cnt: %x reply_sg_cnt:%x "
  391. "dma_reply_sg_cnt: %x\n", bsg_job->request_payload.sg_cnt,
  392. req_sg_cnt, bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
  393. rval = -EAGAIN;
  394. goto done_unmap_sg;
  395. }
  396. if (!vha->flags.online) {
  397. ql_log(ql_log_warn, vha, 0x7012,
  398. "Host is not online.\n");
  399. rval = -EIO;
  400. goto done_unmap_sg;
  401. }
  402. loop_id =
  403. (bsg_job->request->rqst_data.h_ct.preamble_word1 & 0xFF000000)
  404. >> 24;
  405. switch (loop_id) {
  406. case 0xFC:
  407. loop_id = cpu_to_le16(NPH_SNS);
  408. break;
  409. case 0xFA:
  410. loop_id = vha->mgmt_svr_loop_id;
  411. break;
  412. default:
  413. ql_dbg(ql_dbg_user, vha, 0x7013,
  414. "Unknown loop id: %x.\n", loop_id);
  415. rval = -EINVAL;
  416. goto done_unmap_sg;
  417. }
  418. /* Allocate a dummy fcport structure, since functions preparing the
  419. * IOCB and mailbox command retrieves port specific information
  420. * from fcport structure. For Host based ELS commands there will be
  421. * no fcport structure allocated
  422. */
  423. fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
  424. if (!fcport) {
  425. ql_log(ql_log_warn, vha, 0x7014,
  426. "Failed to allocate fcport.\n");
  427. rval = -ENOMEM;
  428. goto done_unmap_sg;
  429. }
  430. /* Initialize all required fields of fcport */
  431. fcport->vha = vha;
  432. fcport->d_id.b.al_pa = bsg_job->request->rqst_data.h_ct.port_id[0];
  433. fcport->d_id.b.area = bsg_job->request->rqst_data.h_ct.port_id[1];
  434. fcport->d_id.b.domain = bsg_job->request->rqst_data.h_ct.port_id[2];
  435. fcport->loop_id = loop_id;
  436. /* Alloc SRB structure */
  437. sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
  438. if (!sp) {
  439. ql_log(ql_log_warn, vha, 0x7015,
  440. "qla2x00_get_sp failed.\n");
  441. rval = -ENOMEM;
  442. goto done_free_fcport;
  443. }
  444. sp->type = SRB_CT_CMD;
  445. sp->name = "bsg_ct";
  446. sp->iocbs = qla24xx_calc_ct_iocbs(req_sg_cnt + rsp_sg_cnt);
  447. sp->u.bsg_job = bsg_job;
  448. sp->free = qla2x00_bsg_sp_free;
  449. sp->done = qla2x00_bsg_job_done;
  450. ql_dbg(ql_dbg_user, vha, 0x7016,
  451. "bsg rqst type: %s else type: %x - "
  452. "loop-id=%x portid=%02x%02x%02x.\n", type,
  453. (bsg_job->request->rqst_data.h_ct.preamble_word2 >> 16),
  454. fcport->loop_id, fcport->d_id.b.domain, fcport->d_id.b.area,
  455. fcport->d_id.b.al_pa);
  456. rval = qla2x00_start_sp(sp);
  457. if (rval != QLA_SUCCESS) {
  458. ql_log(ql_log_warn, vha, 0x7017,
  459. "qla2x00_start_sp failed=%d.\n", rval);
  460. qla2x00_rel_sp(vha, sp);
  461. rval = -EIO;
  462. goto done_free_fcport;
  463. }
  464. return rval;
  465. done_free_fcport:
  466. kfree(fcport);
  467. done_unmap_sg:
  468. dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  469. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  470. dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
  471. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  472. done:
  473. return rval;
  474. }
  475. /* Disable loopback mode */
  476. static inline int
  477. qla81xx_reset_loopback_mode(scsi_qla_host_t *vha, uint16_t *config,
  478. int wait)
  479. {
  480. int ret = 0;
  481. int rval = 0;
  482. uint16_t new_config[4];
  483. struct qla_hw_data *ha = vha->hw;
  484. if (!IS_QLA81XX(ha) && !IS_QLA8031(ha))
  485. goto done_reset_internal;
  486. memset(new_config, 0 , sizeof(new_config));
  487. if ((config[0] & INTERNAL_LOOPBACK_MASK) >> 1 ==
  488. ENABLE_INTERNAL_LOOPBACK ||
  489. (config[0] & INTERNAL_LOOPBACK_MASK) >> 1 ==
  490. ENABLE_EXTERNAL_LOOPBACK) {
  491. new_config[0] = config[0] & ~INTERNAL_LOOPBACK_MASK;
  492. ql_dbg(ql_dbg_user, vha, 0x70bf, "new_config[0]=%02x\n",
  493. (new_config[0] & INTERNAL_LOOPBACK_MASK));
  494. memcpy(&new_config[1], &config[1], sizeof(uint16_t) * 3) ;
  495. ha->notify_dcbx_comp = wait;
  496. ret = qla81xx_set_port_config(vha, new_config);
  497. if (ret != QLA_SUCCESS) {
  498. ql_log(ql_log_warn, vha, 0x7025,
  499. "Set port config failed.\n");
  500. ha->notify_dcbx_comp = 0;
  501. rval = -EINVAL;
  502. goto done_reset_internal;
  503. }
  504. /* Wait for DCBX complete event */
  505. if (wait && !wait_for_completion_timeout(&ha->dcbx_comp,
  506. (20 * HZ))) {
  507. ql_dbg(ql_dbg_user, vha, 0x7026,
  508. "State change notification not received.\n");
  509. ha->notify_dcbx_comp = 0;
  510. rval = -EINVAL;
  511. goto done_reset_internal;
  512. } else
  513. ql_dbg(ql_dbg_user, vha, 0x7027,
  514. "State change received.\n");
  515. ha->notify_dcbx_comp = 0;
  516. }
  517. done_reset_internal:
  518. return rval;
  519. }
  520. /*
  521. * Set the port configuration to enable the internal or external loopback
  522. * depending on the loopback mode.
  523. */
  524. static inline int
  525. qla81xx_set_loopback_mode(scsi_qla_host_t *vha, uint16_t *config,
  526. uint16_t *new_config, uint16_t mode)
  527. {
  528. int ret = 0;
  529. int rval = 0;
  530. struct qla_hw_data *ha = vha->hw;
  531. if (!IS_QLA81XX(ha) && !IS_QLA8031(ha))
  532. goto done_set_internal;
  533. if (mode == INTERNAL_LOOPBACK)
  534. new_config[0] = config[0] | (ENABLE_INTERNAL_LOOPBACK << 1);
  535. else if (mode == EXTERNAL_LOOPBACK)
  536. new_config[0] = config[0] | (ENABLE_EXTERNAL_LOOPBACK << 1);
  537. ql_dbg(ql_dbg_user, vha, 0x70be,
  538. "new_config[0]=%02x\n", (new_config[0] & INTERNAL_LOOPBACK_MASK));
  539. memcpy(&new_config[1], &config[1], sizeof(uint16_t) * 3);
  540. ha->notify_dcbx_comp = 1;
  541. ret = qla81xx_set_port_config(vha, new_config);
  542. if (ret != QLA_SUCCESS) {
  543. ql_log(ql_log_warn, vha, 0x7021,
  544. "set port config failed.\n");
  545. ha->notify_dcbx_comp = 0;
  546. rval = -EINVAL;
  547. goto done_set_internal;
  548. }
  549. /* Wait for DCBX complete event */
  550. if (!wait_for_completion_timeout(&ha->dcbx_comp, (20 * HZ))) {
  551. ql_dbg(ql_dbg_user, vha, 0x7022,
  552. "State change notification not received.\n");
  553. ret = qla81xx_reset_loopback_mode(vha, new_config, 0);
  554. /*
  555. * If the reset of the loopback mode doesn't work take a FCoE
  556. * dump and reset the chip.
  557. */
  558. if (ret) {
  559. ha->isp_ops->fw_dump(vha, 0);
  560. set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
  561. }
  562. rval = -EINVAL;
  563. } else {
  564. if (ha->flags.idc_compl_status) {
  565. ql_dbg(ql_dbg_user, vha, 0x70c3,
  566. "Bad status in IDC Completion AEN\n");
  567. rval = -EINVAL;
  568. ha->flags.idc_compl_status = 0;
  569. } else
  570. ql_dbg(ql_dbg_user, vha, 0x7023,
  571. "State change received.\n");
  572. }
  573. ha->notify_dcbx_comp = 0;
  574. done_set_internal:
  575. return rval;
  576. }
  577. static int
  578. qla2x00_process_loopback(struct fc_bsg_job *bsg_job)
  579. {
  580. struct Scsi_Host *host = bsg_job->shost;
  581. scsi_qla_host_t *vha = shost_priv(host);
  582. struct qla_hw_data *ha = vha->hw;
  583. int rval;
  584. uint8_t command_sent;
  585. char *type;
  586. struct msg_echo_lb elreq;
  587. uint16_t response[MAILBOX_REGISTER_COUNT];
  588. uint16_t config[4], new_config[4];
  589. uint8_t *fw_sts_ptr;
  590. uint8_t *req_data = NULL;
  591. dma_addr_t req_data_dma;
  592. uint32_t req_data_len;
  593. uint8_t *rsp_data = NULL;
  594. dma_addr_t rsp_data_dma;
  595. uint32_t rsp_data_len;
  596. if (!vha->flags.online) {
  597. ql_log(ql_log_warn, vha, 0x7019, "Host is not online.\n");
  598. return -EIO;
  599. }
  600. elreq.req_sg_cnt = dma_map_sg(&ha->pdev->dev,
  601. bsg_job->request_payload.sg_list, bsg_job->request_payload.sg_cnt,
  602. DMA_TO_DEVICE);
  603. if (!elreq.req_sg_cnt) {
  604. ql_log(ql_log_warn, vha, 0x701a,
  605. "dma_map_sg returned %d for request.\n", elreq.req_sg_cnt);
  606. return -ENOMEM;
  607. }
  608. elreq.rsp_sg_cnt = dma_map_sg(&ha->pdev->dev,
  609. bsg_job->reply_payload.sg_list, bsg_job->reply_payload.sg_cnt,
  610. DMA_FROM_DEVICE);
  611. if (!elreq.rsp_sg_cnt) {
  612. ql_log(ql_log_warn, vha, 0x701b,
  613. "dma_map_sg returned %d for reply.\n", elreq.rsp_sg_cnt);
  614. rval = -ENOMEM;
  615. goto done_unmap_req_sg;
  616. }
  617. if ((elreq.req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
  618. (elreq.rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
  619. ql_log(ql_log_warn, vha, 0x701c,
  620. "dma mapping resulted in different sg counts, "
  621. "request_sg_cnt: %x dma_request_sg_cnt: %x "
  622. "reply_sg_cnt: %x dma_reply_sg_cnt: %x.\n",
  623. bsg_job->request_payload.sg_cnt, elreq.req_sg_cnt,
  624. bsg_job->reply_payload.sg_cnt, elreq.rsp_sg_cnt);
  625. rval = -EAGAIN;
  626. goto done_unmap_sg;
  627. }
  628. req_data_len = rsp_data_len = bsg_job->request_payload.payload_len;
  629. req_data = dma_alloc_coherent(&ha->pdev->dev, req_data_len,
  630. &req_data_dma, GFP_KERNEL);
  631. if (!req_data) {
  632. ql_log(ql_log_warn, vha, 0x701d,
  633. "dma alloc failed for req_data.\n");
  634. rval = -ENOMEM;
  635. goto done_unmap_sg;
  636. }
  637. rsp_data = dma_alloc_coherent(&ha->pdev->dev, rsp_data_len,
  638. &rsp_data_dma, GFP_KERNEL);
  639. if (!rsp_data) {
  640. ql_log(ql_log_warn, vha, 0x7004,
  641. "dma alloc failed for rsp_data.\n");
  642. rval = -ENOMEM;
  643. goto done_free_dma_req;
  644. }
  645. /* Copy the request buffer in req_data now */
  646. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  647. bsg_job->request_payload.sg_cnt, req_data, req_data_len);
  648. elreq.send_dma = req_data_dma;
  649. elreq.rcv_dma = rsp_data_dma;
  650. elreq.transfer_size = req_data_len;
  651. elreq.options = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
  652. if (atomic_read(&vha->loop_state) == LOOP_READY &&
  653. (ha->current_topology == ISP_CFG_F ||
  654. ((IS_QLA81XX(ha) || IS_QLA8031(ha)) &&
  655. le32_to_cpu(*(uint32_t *)req_data) == ELS_OPCODE_BYTE
  656. && req_data_len == MAX_ELS_FRAME_PAYLOAD)) &&
  657. elreq.options == EXTERNAL_LOOPBACK) {
  658. type = "FC_BSG_HST_VENDOR_ECHO_DIAG";
  659. ql_dbg(ql_dbg_user, vha, 0x701e,
  660. "BSG request type: %s.\n", type);
  661. command_sent = INT_DEF_LB_ECHO_CMD;
  662. rval = qla2x00_echo_test(vha, &elreq, response);
  663. } else {
  664. if (IS_QLA81XX(ha) || IS_QLA8031(ha)) {
  665. memset(config, 0, sizeof(config));
  666. memset(new_config, 0, sizeof(new_config));
  667. if (qla81xx_get_port_config(vha, config)) {
  668. ql_log(ql_log_warn, vha, 0x701f,
  669. "Get port config failed.\n");
  670. rval = -EPERM;
  671. goto done_free_dma_rsp;
  672. }
  673. if ((config[0] & INTERNAL_LOOPBACK_MASK) != 0) {
  674. ql_dbg(ql_dbg_user, vha, 0x70c4,
  675. "Loopback operation already in "
  676. "progress.\n");
  677. rval = -EAGAIN;
  678. goto done_free_dma_rsp;
  679. }
  680. ql_dbg(ql_dbg_user, vha, 0x70c0,
  681. "elreq.options=%04x\n", elreq.options);
  682. if (elreq.options == EXTERNAL_LOOPBACK)
  683. if (IS_QLA8031(ha))
  684. rval = qla81xx_set_loopback_mode(vha,
  685. config, new_config, elreq.options);
  686. else
  687. rval = qla81xx_reset_loopback_mode(vha,
  688. config, 1);
  689. else
  690. rval = qla81xx_set_loopback_mode(vha, config,
  691. new_config, elreq.options);
  692. if (rval) {
  693. rval = -EPERM;
  694. goto done_free_dma_rsp;
  695. }
  696. type = "FC_BSG_HST_VENDOR_LOOPBACK";
  697. ql_dbg(ql_dbg_user, vha, 0x7028,
  698. "BSG request type: %s.\n", type);
  699. command_sent = INT_DEF_LB_LOOPBACK_CMD;
  700. rval = qla2x00_loopback_test(vha, &elreq, response);
  701. if (response[0] == MBS_COMMAND_ERROR &&
  702. response[1] == MBS_LB_RESET) {
  703. ql_log(ql_log_warn, vha, 0x7029,
  704. "MBX command error, Aborting ISP.\n");
  705. set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
  706. qla2xxx_wake_dpc(vha);
  707. qla2x00_wait_for_chip_reset(vha);
  708. /* Also reset the MPI */
  709. if (IS_QLA81XX(ha)) {
  710. if (qla81xx_restart_mpi_firmware(vha) !=
  711. QLA_SUCCESS) {
  712. ql_log(ql_log_warn, vha, 0x702a,
  713. "MPI reset failed.\n");
  714. }
  715. }
  716. rval = -EIO;
  717. goto done_free_dma_rsp;
  718. }
  719. if (new_config[0]) {
  720. int ret;
  721. /* Revert back to original port config
  722. * Also clear internal loopback
  723. */
  724. ret = qla81xx_reset_loopback_mode(vha,
  725. new_config, 0);
  726. if (ret) {
  727. /*
  728. * If the reset of the loopback mode
  729. * doesn't work take FCoE dump and then
  730. * reset the chip.
  731. */
  732. ha->isp_ops->fw_dump(vha, 0);
  733. set_bit(ISP_ABORT_NEEDED,
  734. &vha->dpc_flags);
  735. }
  736. }
  737. } else {
  738. type = "FC_BSG_HST_VENDOR_LOOPBACK";
  739. ql_dbg(ql_dbg_user, vha, 0x702b,
  740. "BSG request type: %s.\n", type);
  741. command_sent = INT_DEF_LB_LOOPBACK_CMD;
  742. rval = qla2x00_loopback_test(vha, &elreq, response);
  743. }
  744. }
  745. if (rval) {
  746. ql_log(ql_log_warn, vha, 0x702c,
  747. "Vendor request %s failed.\n", type);
  748. rval = 0;
  749. bsg_job->reply->result = (DID_ERROR << 16);
  750. bsg_job->reply->reply_payload_rcv_len = 0;
  751. } else {
  752. ql_dbg(ql_dbg_user, vha, 0x702d,
  753. "Vendor request %s completed.\n", type);
  754. bsg_job->reply->result = (DID_OK << 16);
  755. sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
  756. bsg_job->reply_payload.sg_cnt, rsp_data,
  757. rsp_data_len);
  758. }
  759. bsg_job->reply_len = sizeof(struct fc_bsg_reply) +
  760. sizeof(response) + sizeof(uint8_t);
  761. fw_sts_ptr = ((uint8_t *)bsg_job->req->sense) +
  762. sizeof(struct fc_bsg_reply);
  763. memcpy(fw_sts_ptr, response, sizeof(response));
  764. fw_sts_ptr += sizeof(response);
  765. *fw_sts_ptr = command_sent;
  766. done_free_dma_rsp:
  767. dma_free_coherent(&ha->pdev->dev, rsp_data_len,
  768. rsp_data, rsp_data_dma);
  769. done_free_dma_req:
  770. dma_free_coherent(&ha->pdev->dev, req_data_len,
  771. req_data, req_data_dma);
  772. done_unmap_sg:
  773. dma_unmap_sg(&ha->pdev->dev,
  774. bsg_job->reply_payload.sg_list,
  775. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  776. done_unmap_req_sg:
  777. dma_unmap_sg(&ha->pdev->dev,
  778. bsg_job->request_payload.sg_list,
  779. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  780. if (!rval)
  781. bsg_job->job_done(bsg_job);
  782. return rval;
  783. }
  784. static int
  785. qla84xx_reset(struct fc_bsg_job *bsg_job)
  786. {
  787. struct Scsi_Host *host = bsg_job->shost;
  788. scsi_qla_host_t *vha = shost_priv(host);
  789. struct qla_hw_data *ha = vha->hw;
  790. int rval = 0;
  791. uint32_t flag;
  792. if (!IS_QLA84XX(ha)) {
  793. ql_dbg(ql_dbg_user, vha, 0x702f, "Not 84xx, exiting.\n");
  794. return -EINVAL;
  795. }
  796. flag = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
  797. rval = qla84xx_reset_chip(vha, flag == A84_ISSUE_RESET_DIAG_FW);
  798. if (rval) {
  799. ql_log(ql_log_warn, vha, 0x7030,
  800. "Vendor request 84xx reset failed.\n");
  801. rval = (DID_ERROR << 16);
  802. } else {
  803. ql_dbg(ql_dbg_user, vha, 0x7031,
  804. "Vendor request 84xx reset completed.\n");
  805. bsg_job->reply->result = DID_OK;
  806. bsg_job->job_done(bsg_job);
  807. }
  808. return rval;
  809. }
  810. static int
  811. qla84xx_updatefw(struct fc_bsg_job *bsg_job)
  812. {
  813. struct Scsi_Host *host = bsg_job->shost;
  814. scsi_qla_host_t *vha = shost_priv(host);
  815. struct qla_hw_data *ha = vha->hw;
  816. struct verify_chip_entry_84xx *mn = NULL;
  817. dma_addr_t mn_dma, fw_dma;
  818. void *fw_buf = NULL;
  819. int rval = 0;
  820. uint32_t sg_cnt;
  821. uint32_t data_len;
  822. uint16_t options;
  823. uint32_t flag;
  824. uint32_t fw_ver;
  825. if (!IS_QLA84XX(ha)) {
  826. ql_dbg(ql_dbg_user, vha, 0x7032,
  827. "Not 84xx, exiting.\n");
  828. return -EINVAL;
  829. }
  830. sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  831. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  832. if (!sg_cnt) {
  833. ql_log(ql_log_warn, vha, 0x7033,
  834. "dma_map_sg returned %d for request.\n", sg_cnt);
  835. return -ENOMEM;
  836. }
  837. if (sg_cnt != bsg_job->request_payload.sg_cnt) {
  838. ql_log(ql_log_warn, vha, 0x7034,
  839. "DMA mapping resulted in different sg counts, "
  840. "request_sg_cnt: %x dma_request_sg_cnt: %x.\n",
  841. bsg_job->request_payload.sg_cnt, sg_cnt);
  842. rval = -EAGAIN;
  843. goto done_unmap_sg;
  844. }
  845. data_len = bsg_job->request_payload.payload_len;
  846. fw_buf = dma_alloc_coherent(&ha->pdev->dev, data_len,
  847. &fw_dma, GFP_KERNEL);
  848. if (!fw_buf) {
  849. ql_log(ql_log_warn, vha, 0x7035,
  850. "DMA alloc failed for fw_buf.\n");
  851. rval = -ENOMEM;
  852. goto done_unmap_sg;
  853. }
  854. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  855. bsg_job->request_payload.sg_cnt, fw_buf, data_len);
  856. mn = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &mn_dma);
  857. if (!mn) {
  858. ql_log(ql_log_warn, vha, 0x7036,
  859. "DMA alloc failed for fw buffer.\n");
  860. rval = -ENOMEM;
  861. goto done_free_fw_buf;
  862. }
  863. flag = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
  864. fw_ver = le32_to_cpu(*((uint32_t *)((uint32_t *)fw_buf + 2)));
  865. memset(mn, 0, sizeof(struct access_chip_84xx));
  866. mn->entry_type = VERIFY_CHIP_IOCB_TYPE;
  867. mn->entry_count = 1;
  868. options = VCO_FORCE_UPDATE | VCO_END_OF_DATA;
  869. if (flag == A84_ISSUE_UPDATE_DIAGFW_CMD)
  870. options |= VCO_DIAG_FW;
  871. mn->options = cpu_to_le16(options);
  872. mn->fw_ver = cpu_to_le32(fw_ver);
  873. mn->fw_size = cpu_to_le32(data_len);
  874. mn->fw_seq_size = cpu_to_le32(data_len);
  875. mn->dseg_address[0] = cpu_to_le32(LSD(fw_dma));
  876. mn->dseg_address[1] = cpu_to_le32(MSD(fw_dma));
  877. mn->dseg_length = cpu_to_le32(data_len);
  878. mn->data_seg_cnt = cpu_to_le16(1);
  879. rval = qla2x00_issue_iocb_timeout(vha, mn, mn_dma, 0, 120);
  880. if (rval) {
  881. ql_log(ql_log_warn, vha, 0x7037,
  882. "Vendor request 84xx updatefw failed.\n");
  883. rval = (DID_ERROR << 16);
  884. } else {
  885. ql_dbg(ql_dbg_user, vha, 0x7038,
  886. "Vendor request 84xx updatefw completed.\n");
  887. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  888. bsg_job->reply->result = DID_OK;
  889. }
  890. dma_pool_free(ha->s_dma_pool, mn, mn_dma);
  891. done_free_fw_buf:
  892. dma_free_coherent(&ha->pdev->dev, data_len, fw_buf, fw_dma);
  893. done_unmap_sg:
  894. dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  895. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  896. if (!rval)
  897. bsg_job->job_done(bsg_job);
  898. return rval;
  899. }
  900. static int
  901. qla84xx_mgmt_cmd(struct fc_bsg_job *bsg_job)
  902. {
  903. struct Scsi_Host *host = bsg_job->shost;
  904. scsi_qla_host_t *vha = shost_priv(host);
  905. struct qla_hw_data *ha = vha->hw;
  906. struct access_chip_84xx *mn = NULL;
  907. dma_addr_t mn_dma, mgmt_dma;
  908. void *mgmt_b = NULL;
  909. int rval = 0;
  910. struct qla_bsg_a84_mgmt *ql84_mgmt;
  911. uint32_t sg_cnt;
  912. uint32_t data_len = 0;
  913. uint32_t dma_direction = DMA_NONE;
  914. if (!IS_QLA84XX(ha)) {
  915. ql_log(ql_log_warn, vha, 0x703a,
  916. "Not 84xx, exiting.\n");
  917. return -EINVAL;
  918. }
  919. ql84_mgmt = (struct qla_bsg_a84_mgmt *)((char *)bsg_job->request +
  920. sizeof(struct fc_bsg_request));
  921. if (!ql84_mgmt) {
  922. ql_log(ql_log_warn, vha, 0x703b,
  923. "MGMT header not provided, exiting.\n");
  924. return -EINVAL;
  925. }
  926. mn = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &mn_dma);
  927. if (!mn) {
  928. ql_log(ql_log_warn, vha, 0x703c,
  929. "DMA alloc failed for fw buffer.\n");
  930. return -ENOMEM;
  931. }
  932. memset(mn, 0, sizeof(struct access_chip_84xx));
  933. mn->entry_type = ACCESS_CHIP_IOCB_TYPE;
  934. mn->entry_count = 1;
  935. switch (ql84_mgmt->mgmt.cmd) {
  936. case QLA84_MGMT_READ_MEM:
  937. case QLA84_MGMT_GET_INFO:
  938. sg_cnt = dma_map_sg(&ha->pdev->dev,
  939. bsg_job->reply_payload.sg_list,
  940. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  941. if (!sg_cnt) {
  942. ql_log(ql_log_warn, vha, 0x703d,
  943. "dma_map_sg returned %d for reply.\n", sg_cnt);
  944. rval = -ENOMEM;
  945. goto exit_mgmt;
  946. }
  947. dma_direction = DMA_FROM_DEVICE;
  948. if (sg_cnt != bsg_job->reply_payload.sg_cnt) {
  949. ql_log(ql_log_warn, vha, 0x703e,
  950. "DMA mapping resulted in different sg counts, "
  951. "reply_sg_cnt: %x dma_reply_sg_cnt: %x.\n",
  952. bsg_job->reply_payload.sg_cnt, sg_cnt);
  953. rval = -EAGAIN;
  954. goto done_unmap_sg;
  955. }
  956. data_len = bsg_job->reply_payload.payload_len;
  957. mgmt_b = dma_alloc_coherent(&ha->pdev->dev, data_len,
  958. &mgmt_dma, GFP_KERNEL);
  959. if (!mgmt_b) {
  960. ql_log(ql_log_warn, vha, 0x703f,
  961. "DMA alloc failed for mgmt_b.\n");
  962. rval = -ENOMEM;
  963. goto done_unmap_sg;
  964. }
  965. if (ql84_mgmt->mgmt.cmd == QLA84_MGMT_READ_MEM) {
  966. mn->options = cpu_to_le16(ACO_DUMP_MEMORY);
  967. mn->parameter1 =
  968. cpu_to_le32(
  969. ql84_mgmt->mgmt.mgmtp.u.mem.start_addr);
  970. } else if (ql84_mgmt->mgmt.cmd == QLA84_MGMT_GET_INFO) {
  971. mn->options = cpu_to_le16(ACO_REQUEST_INFO);
  972. mn->parameter1 =
  973. cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.info.type);
  974. mn->parameter2 =
  975. cpu_to_le32(
  976. ql84_mgmt->mgmt.mgmtp.u.info.context);
  977. }
  978. break;
  979. case QLA84_MGMT_WRITE_MEM:
  980. sg_cnt = dma_map_sg(&ha->pdev->dev,
  981. bsg_job->request_payload.sg_list,
  982. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  983. if (!sg_cnt) {
  984. ql_log(ql_log_warn, vha, 0x7040,
  985. "dma_map_sg returned %d.\n", sg_cnt);
  986. rval = -ENOMEM;
  987. goto exit_mgmt;
  988. }
  989. dma_direction = DMA_TO_DEVICE;
  990. if (sg_cnt != bsg_job->request_payload.sg_cnt) {
  991. ql_log(ql_log_warn, vha, 0x7041,
  992. "DMA mapping resulted in different sg counts, "
  993. "request_sg_cnt: %x dma_request_sg_cnt: %x.\n",
  994. bsg_job->request_payload.sg_cnt, sg_cnt);
  995. rval = -EAGAIN;
  996. goto done_unmap_sg;
  997. }
  998. data_len = bsg_job->request_payload.payload_len;
  999. mgmt_b = dma_alloc_coherent(&ha->pdev->dev, data_len,
  1000. &mgmt_dma, GFP_KERNEL);
  1001. if (!mgmt_b) {
  1002. ql_log(ql_log_warn, vha, 0x7042,
  1003. "DMA alloc failed for mgmt_b.\n");
  1004. rval = -ENOMEM;
  1005. goto done_unmap_sg;
  1006. }
  1007. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  1008. bsg_job->request_payload.sg_cnt, mgmt_b, data_len);
  1009. mn->options = cpu_to_le16(ACO_LOAD_MEMORY);
  1010. mn->parameter1 =
  1011. cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.mem.start_addr);
  1012. break;
  1013. case QLA84_MGMT_CHNG_CONFIG:
  1014. mn->options = cpu_to_le16(ACO_CHANGE_CONFIG_PARAM);
  1015. mn->parameter1 =
  1016. cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.id);
  1017. mn->parameter2 =
  1018. cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.param0);
  1019. mn->parameter3 =
  1020. cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.param1);
  1021. break;
  1022. default:
  1023. rval = -EIO;
  1024. goto exit_mgmt;
  1025. }
  1026. if (ql84_mgmt->mgmt.cmd != QLA84_MGMT_CHNG_CONFIG) {
  1027. mn->total_byte_cnt = cpu_to_le32(ql84_mgmt->mgmt.len);
  1028. mn->dseg_count = cpu_to_le16(1);
  1029. mn->dseg_address[0] = cpu_to_le32(LSD(mgmt_dma));
  1030. mn->dseg_address[1] = cpu_to_le32(MSD(mgmt_dma));
  1031. mn->dseg_length = cpu_to_le32(ql84_mgmt->mgmt.len);
  1032. }
  1033. rval = qla2x00_issue_iocb(vha, mn, mn_dma, 0);
  1034. if (rval) {
  1035. ql_log(ql_log_warn, vha, 0x7043,
  1036. "Vendor request 84xx mgmt failed.\n");
  1037. rval = (DID_ERROR << 16);
  1038. } else {
  1039. ql_dbg(ql_dbg_user, vha, 0x7044,
  1040. "Vendor request 84xx mgmt completed.\n");
  1041. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  1042. bsg_job->reply->result = DID_OK;
  1043. if ((ql84_mgmt->mgmt.cmd == QLA84_MGMT_READ_MEM) ||
  1044. (ql84_mgmt->mgmt.cmd == QLA84_MGMT_GET_INFO)) {
  1045. bsg_job->reply->reply_payload_rcv_len =
  1046. bsg_job->reply_payload.payload_len;
  1047. sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
  1048. bsg_job->reply_payload.sg_cnt, mgmt_b,
  1049. data_len);
  1050. }
  1051. }
  1052. done_unmap_sg:
  1053. if (mgmt_b)
  1054. dma_free_coherent(&ha->pdev->dev, data_len, mgmt_b, mgmt_dma);
  1055. if (dma_direction == DMA_TO_DEVICE)
  1056. dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  1057. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  1058. else if (dma_direction == DMA_FROM_DEVICE)
  1059. dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
  1060. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  1061. exit_mgmt:
  1062. dma_pool_free(ha->s_dma_pool, mn, mn_dma);
  1063. if (!rval)
  1064. bsg_job->job_done(bsg_job);
  1065. return rval;
  1066. }
  1067. static int
  1068. qla24xx_iidma(struct fc_bsg_job *bsg_job)
  1069. {
  1070. struct Scsi_Host *host = bsg_job->shost;
  1071. scsi_qla_host_t *vha = shost_priv(host);
  1072. int rval = 0;
  1073. struct qla_port_param *port_param = NULL;
  1074. fc_port_t *fcport = NULL;
  1075. uint16_t mb[MAILBOX_REGISTER_COUNT];
  1076. uint8_t *rsp_ptr = NULL;
  1077. if (!IS_IIDMA_CAPABLE(vha->hw)) {
  1078. ql_log(ql_log_info, vha, 0x7046, "iiDMA not supported.\n");
  1079. return -EINVAL;
  1080. }
  1081. port_param = (struct qla_port_param *)((char *)bsg_job->request +
  1082. sizeof(struct fc_bsg_request));
  1083. if (!port_param) {
  1084. ql_log(ql_log_warn, vha, 0x7047,
  1085. "port_param header not provided.\n");
  1086. return -EINVAL;
  1087. }
  1088. if (port_param->fc_scsi_addr.dest_type != EXT_DEF_TYPE_WWPN) {
  1089. ql_log(ql_log_warn, vha, 0x7048,
  1090. "Invalid destination type.\n");
  1091. return -EINVAL;
  1092. }
  1093. list_for_each_entry(fcport, &vha->vp_fcports, list) {
  1094. if (fcport->port_type != FCT_TARGET)
  1095. continue;
  1096. if (memcmp(port_param->fc_scsi_addr.dest_addr.wwpn,
  1097. fcport->port_name, sizeof(fcport->port_name)))
  1098. continue;
  1099. break;
  1100. }
  1101. if (!fcport) {
  1102. ql_log(ql_log_warn, vha, 0x7049,
  1103. "Failed to find port.\n");
  1104. return -EINVAL;
  1105. }
  1106. if (atomic_read(&fcport->state) != FCS_ONLINE) {
  1107. ql_log(ql_log_warn, vha, 0x704a,
  1108. "Port is not online.\n");
  1109. return -EINVAL;
  1110. }
  1111. if (fcport->flags & FCF_LOGIN_NEEDED) {
  1112. ql_log(ql_log_warn, vha, 0x704b,
  1113. "Remote port not logged in flags = 0x%x.\n", fcport->flags);
  1114. return -EINVAL;
  1115. }
  1116. if (port_param->mode)
  1117. rval = qla2x00_set_idma_speed(vha, fcport->loop_id,
  1118. port_param->speed, mb);
  1119. else
  1120. rval = qla2x00_get_idma_speed(vha, fcport->loop_id,
  1121. &port_param->speed, mb);
  1122. if (rval) {
  1123. ql_log(ql_log_warn, vha, 0x704c,
  1124. "iIDMA cmd failed for %02x%02x%02x%02x%02x%02x%02x%02x -- "
  1125. "%04x %x %04x %04x.\n", fcport->port_name[0],
  1126. fcport->port_name[1], fcport->port_name[2],
  1127. fcport->port_name[3], fcport->port_name[4],
  1128. fcport->port_name[5], fcport->port_name[6],
  1129. fcport->port_name[7], rval, fcport->fp_speed, mb[0], mb[1]);
  1130. rval = (DID_ERROR << 16);
  1131. } else {
  1132. if (!port_param->mode) {
  1133. bsg_job->reply_len = sizeof(struct fc_bsg_reply) +
  1134. sizeof(struct qla_port_param);
  1135. rsp_ptr = ((uint8_t *)bsg_job->reply) +
  1136. sizeof(struct fc_bsg_reply);
  1137. memcpy(rsp_ptr, port_param,
  1138. sizeof(struct qla_port_param));
  1139. }
  1140. bsg_job->reply->result = DID_OK;
  1141. bsg_job->job_done(bsg_job);
  1142. }
  1143. return rval;
  1144. }
  1145. static int
  1146. qla2x00_optrom_setup(struct fc_bsg_job *bsg_job, scsi_qla_host_t *vha,
  1147. uint8_t is_update)
  1148. {
  1149. uint32_t start = 0;
  1150. int valid = 0;
  1151. struct qla_hw_data *ha = vha->hw;
  1152. if (unlikely(pci_channel_offline(ha->pdev)))
  1153. return -EINVAL;
  1154. start = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
  1155. if (start > ha->optrom_size) {
  1156. ql_log(ql_log_warn, vha, 0x7055,
  1157. "start %d > optrom_size %d.\n", start, ha->optrom_size);
  1158. return -EINVAL;
  1159. }
  1160. if (ha->optrom_state != QLA_SWAITING) {
  1161. ql_log(ql_log_info, vha, 0x7056,
  1162. "optrom_state %d.\n", ha->optrom_state);
  1163. return -EBUSY;
  1164. }
  1165. ha->optrom_region_start = start;
  1166. ql_dbg(ql_dbg_user, vha, 0x7057, "is_update=%d.\n", is_update);
  1167. if (is_update) {
  1168. if (ha->optrom_size == OPTROM_SIZE_2300 && start == 0)
  1169. valid = 1;
  1170. else if (start == (ha->flt_region_boot * 4) ||
  1171. start == (ha->flt_region_fw * 4))
  1172. valid = 1;
  1173. else if (IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha) ||
  1174. IS_CNA_CAPABLE(ha) || IS_QLA2031(ha))
  1175. valid = 1;
  1176. if (!valid) {
  1177. ql_log(ql_log_warn, vha, 0x7058,
  1178. "Invalid start region 0x%x/0x%x.\n", start,
  1179. bsg_job->request_payload.payload_len);
  1180. return -EINVAL;
  1181. }
  1182. ha->optrom_region_size = start +
  1183. bsg_job->request_payload.payload_len > ha->optrom_size ?
  1184. ha->optrom_size - start :
  1185. bsg_job->request_payload.payload_len;
  1186. ha->optrom_state = QLA_SWRITING;
  1187. } else {
  1188. ha->optrom_region_size = start +
  1189. bsg_job->reply_payload.payload_len > ha->optrom_size ?
  1190. ha->optrom_size - start :
  1191. bsg_job->reply_payload.payload_len;
  1192. ha->optrom_state = QLA_SREADING;
  1193. }
  1194. ha->optrom_buffer = vmalloc(ha->optrom_region_size);
  1195. if (!ha->optrom_buffer) {
  1196. ql_log(ql_log_warn, vha, 0x7059,
  1197. "Read: Unable to allocate memory for optrom retrieval "
  1198. "(%x)\n", ha->optrom_region_size);
  1199. ha->optrom_state = QLA_SWAITING;
  1200. return -ENOMEM;
  1201. }
  1202. memset(ha->optrom_buffer, 0, ha->optrom_region_size);
  1203. return 0;
  1204. }
  1205. static int
  1206. qla2x00_read_optrom(struct fc_bsg_job *bsg_job)
  1207. {
  1208. struct Scsi_Host *host = bsg_job->shost;
  1209. scsi_qla_host_t *vha = shost_priv(host);
  1210. struct qla_hw_data *ha = vha->hw;
  1211. int rval = 0;
  1212. if (ha->flags.nic_core_reset_hdlr_active)
  1213. return -EBUSY;
  1214. rval = qla2x00_optrom_setup(bsg_job, vha, 0);
  1215. if (rval)
  1216. return rval;
  1217. ha->isp_ops->read_optrom(vha, ha->optrom_buffer,
  1218. ha->optrom_region_start, ha->optrom_region_size);
  1219. sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
  1220. bsg_job->reply_payload.sg_cnt, ha->optrom_buffer,
  1221. ha->optrom_region_size);
  1222. bsg_job->reply->reply_payload_rcv_len = ha->optrom_region_size;
  1223. bsg_job->reply->result = DID_OK;
  1224. vfree(ha->optrom_buffer);
  1225. ha->optrom_buffer = NULL;
  1226. ha->optrom_state = QLA_SWAITING;
  1227. bsg_job->job_done(bsg_job);
  1228. return rval;
  1229. }
  1230. static int
  1231. qla2x00_update_optrom(struct fc_bsg_job *bsg_job)
  1232. {
  1233. struct Scsi_Host *host = bsg_job->shost;
  1234. scsi_qla_host_t *vha = shost_priv(host);
  1235. struct qla_hw_data *ha = vha->hw;
  1236. int rval = 0;
  1237. rval = qla2x00_optrom_setup(bsg_job, vha, 1);
  1238. if (rval)
  1239. return rval;
  1240. /* Set the isp82xx_no_md_cap not to capture minidump */
  1241. ha->flags.isp82xx_no_md_cap = 1;
  1242. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  1243. bsg_job->request_payload.sg_cnt, ha->optrom_buffer,
  1244. ha->optrom_region_size);
  1245. ha->isp_ops->write_optrom(vha, ha->optrom_buffer,
  1246. ha->optrom_region_start, ha->optrom_region_size);
  1247. bsg_job->reply->result = DID_OK;
  1248. vfree(ha->optrom_buffer);
  1249. ha->optrom_buffer = NULL;
  1250. ha->optrom_state = QLA_SWAITING;
  1251. bsg_job->job_done(bsg_job);
  1252. return rval;
  1253. }
  1254. static int
  1255. qla2x00_update_fru_versions(struct fc_bsg_job *bsg_job)
  1256. {
  1257. struct Scsi_Host *host = bsg_job->shost;
  1258. scsi_qla_host_t *vha = shost_priv(host);
  1259. struct qla_hw_data *ha = vha->hw;
  1260. int rval = 0;
  1261. uint8_t bsg[DMA_POOL_SIZE];
  1262. struct qla_image_version_list *list = (void *)bsg;
  1263. struct qla_image_version *image;
  1264. uint32_t count;
  1265. dma_addr_t sfp_dma;
  1266. void *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
  1267. if (!sfp) {
  1268. bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
  1269. EXT_STATUS_NO_MEMORY;
  1270. goto done;
  1271. }
  1272. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  1273. bsg_job->request_payload.sg_cnt, list, sizeof(bsg));
  1274. image = list->version;
  1275. count = list->count;
  1276. while (count--) {
  1277. memcpy(sfp, &image->field_info, sizeof(image->field_info));
  1278. rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
  1279. image->field_address.device, image->field_address.offset,
  1280. sizeof(image->field_info), image->field_address.option);
  1281. if (rval) {
  1282. bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
  1283. EXT_STATUS_MAILBOX;
  1284. goto dealloc;
  1285. }
  1286. image++;
  1287. }
  1288. bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
  1289. dealloc:
  1290. dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
  1291. done:
  1292. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  1293. bsg_job->reply->result = DID_OK << 16;
  1294. bsg_job->job_done(bsg_job);
  1295. return 0;
  1296. }
  1297. static int
  1298. qla2x00_read_fru_status(struct fc_bsg_job *bsg_job)
  1299. {
  1300. struct Scsi_Host *host = bsg_job->shost;
  1301. scsi_qla_host_t *vha = shost_priv(host);
  1302. struct qla_hw_data *ha = vha->hw;
  1303. int rval = 0;
  1304. uint8_t bsg[DMA_POOL_SIZE];
  1305. struct qla_status_reg *sr = (void *)bsg;
  1306. dma_addr_t sfp_dma;
  1307. uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
  1308. if (!sfp) {
  1309. bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
  1310. EXT_STATUS_NO_MEMORY;
  1311. goto done;
  1312. }
  1313. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  1314. bsg_job->request_payload.sg_cnt, sr, sizeof(*sr));
  1315. rval = qla2x00_read_sfp(vha, sfp_dma, sfp,
  1316. sr->field_address.device, sr->field_address.offset,
  1317. sizeof(sr->status_reg), sr->field_address.option);
  1318. sr->status_reg = *sfp;
  1319. if (rval) {
  1320. bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
  1321. EXT_STATUS_MAILBOX;
  1322. goto dealloc;
  1323. }
  1324. sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
  1325. bsg_job->reply_payload.sg_cnt, sr, sizeof(*sr));
  1326. bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
  1327. dealloc:
  1328. dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
  1329. done:
  1330. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  1331. bsg_job->reply->reply_payload_rcv_len = sizeof(*sr);
  1332. bsg_job->reply->result = DID_OK << 16;
  1333. bsg_job->job_done(bsg_job);
  1334. return 0;
  1335. }
  1336. static int
  1337. qla2x00_write_fru_status(struct fc_bsg_job *bsg_job)
  1338. {
  1339. struct Scsi_Host *host = bsg_job->shost;
  1340. scsi_qla_host_t *vha = shost_priv(host);
  1341. struct qla_hw_data *ha = vha->hw;
  1342. int rval = 0;
  1343. uint8_t bsg[DMA_POOL_SIZE];
  1344. struct qla_status_reg *sr = (void *)bsg;
  1345. dma_addr_t sfp_dma;
  1346. uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
  1347. if (!sfp) {
  1348. bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
  1349. EXT_STATUS_NO_MEMORY;
  1350. goto done;
  1351. }
  1352. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  1353. bsg_job->request_payload.sg_cnt, sr, sizeof(*sr));
  1354. *sfp = sr->status_reg;
  1355. rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
  1356. sr->field_address.device, sr->field_address.offset,
  1357. sizeof(sr->status_reg), sr->field_address.option);
  1358. if (rval) {
  1359. bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
  1360. EXT_STATUS_MAILBOX;
  1361. goto dealloc;
  1362. }
  1363. bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
  1364. dealloc:
  1365. dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
  1366. done:
  1367. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  1368. bsg_job->reply->result = DID_OK << 16;
  1369. bsg_job->job_done(bsg_job);
  1370. return 0;
  1371. }
  1372. static int
  1373. qla2x00_write_i2c(struct fc_bsg_job *bsg_job)
  1374. {
  1375. struct Scsi_Host *host = bsg_job->shost;
  1376. scsi_qla_host_t *vha = shost_priv(host);
  1377. struct qla_hw_data *ha = vha->hw;
  1378. int rval = 0;
  1379. uint8_t bsg[DMA_POOL_SIZE];
  1380. struct qla_i2c_access *i2c = (void *)bsg;
  1381. dma_addr_t sfp_dma;
  1382. uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
  1383. if (!sfp) {
  1384. bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
  1385. EXT_STATUS_NO_MEMORY;
  1386. goto done;
  1387. }
  1388. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  1389. bsg_job->request_payload.sg_cnt, i2c, sizeof(*i2c));
  1390. memcpy(sfp, i2c->buffer, i2c->length);
  1391. rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
  1392. i2c->device, i2c->offset, i2c->length, i2c->option);
  1393. if (rval) {
  1394. bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
  1395. EXT_STATUS_MAILBOX;
  1396. goto dealloc;
  1397. }
  1398. bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
  1399. dealloc:
  1400. dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
  1401. done:
  1402. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  1403. bsg_job->reply->result = DID_OK << 16;
  1404. bsg_job->job_done(bsg_job);
  1405. return 0;
  1406. }
  1407. static int
  1408. qla2x00_read_i2c(struct fc_bsg_job *bsg_job)
  1409. {
  1410. struct Scsi_Host *host = bsg_job->shost;
  1411. scsi_qla_host_t *vha = shost_priv(host);
  1412. struct qla_hw_data *ha = vha->hw;
  1413. int rval = 0;
  1414. uint8_t bsg[DMA_POOL_SIZE];
  1415. struct qla_i2c_access *i2c = (void *)bsg;
  1416. dma_addr_t sfp_dma;
  1417. uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
  1418. if (!sfp) {
  1419. bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
  1420. EXT_STATUS_NO_MEMORY;
  1421. goto done;
  1422. }
  1423. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  1424. bsg_job->request_payload.sg_cnt, i2c, sizeof(*i2c));
  1425. rval = qla2x00_read_sfp(vha, sfp_dma, sfp,
  1426. i2c->device, i2c->offset, i2c->length, i2c->option);
  1427. if (rval) {
  1428. bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
  1429. EXT_STATUS_MAILBOX;
  1430. goto dealloc;
  1431. }
  1432. memcpy(i2c->buffer, sfp, i2c->length);
  1433. sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
  1434. bsg_job->reply_payload.sg_cnt, i2c, sizeof(*i2c));
  1435. bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
  1436. dealloc:
  1437. dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
  1438. done:
  1439. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  1440. bsg_job->reply->reply_payload_rcv_len = sizeof(*i2c);
  1441. bsg_job->reply->result = DID_OK << 16;
  1442. bsg_job->job_done(bsg_job);
  1443. return 0;
  1444. }
  1445. static int
  1446. qla24xx_process_bidir_cmd(struct fc_bsg_job *bsg_job)
  1447. {
  1448. struct Scsi_Host *host = bsg_job->shost;
  1449. scsi_qla_host_t *vha = shost_priv(host);
  1450. struct qla_hw_data *ha = vha->hw;
  1451. uint16_t thread_id;
  1452. uint32_t rval = EXT_STATUS_OK;
  1453. uint16_t req_sg_cnt = 0;
  1454. uint16_t rsp_sg_cnt = 0;
  1455. uint16_t nextlid = 0;
  1456. uint32_t tot_dsds;
  1457. srb_t *sp = NULL;
  1458. uint32_t req_data_len = 0;
  1459. uint32_t rsp_data_len = 0;
  1460. /* Check the type of the adapter */
  1461. if (!IS_BIDI_CAPABLE(ha)) {
  1462. ql_log(ql_log_warn, vha, 0x70a0,
  1463. "This adapter is not supported\n");
  1464. rval = EXT_STATUS_NOT_SUPPORTED;
  1465. goto done;
  1466. }
  1467. if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
  1468. test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags) ||
  1469. test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
  1470. rval = EXT_STATUS_BUSY;
  1471. goto done;
  1472. }
  1473. /* Check if host is online */
  1474. if (!vha->flags.online) {
  1475. ql_log(ql_log_warn, vha, 0x70a1,
  1476. "Host is not online\n");
  1477. rval = EXT_STATUS_DEVICE_OFFLINE;
  1478. goto done;
  1479. }
  1480. /* Check if cable is plugged in or not */
  1481. if (vha->device_flags & DFLG_NO_CABLE) {
  1482. ql_log(ql_log_warn, vha, 0x70a2,
  1483. "Cable is unplugged...\n");
  1484. rval = EXT_STATUS_INVALID_CFG;
  1485. goto done;
  1486. }
  1487. /* Check if the switch is connected or not */
  1488. if (ha->current_topology != ISP_CFG_F) {
  1489. ql_log(ql_log_warn, vha, 0x70a3,
  1490. "Host is not connected to the switch\n");
  1491. rval = EXT_STATUS_INVALID_CFG;
  1492. goto done;
  1493. }
  1494. /* Check if operating mode is P2P */
  1495. if (ha->operating_mode != P2P) {
  1496. ql_log(ql_log_warn, vha, 0x70a4,
  1497. "Host is operating mode is not P2p\n");
  1498. rval = EXT_STATUS_INVALID_CFG;
  1499. goto done;
  1500. }
  1501. thread_id = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
  1502. mutex_lock(&ha->selflogin_lock);
  1503. if (vha->self_login_loop_id == 0) {
  1504. /* Initialize all required fields of fcport */
  1505. vha->bidir_fcport.vha = vha;
  1506. vha->bidir_fcport.d_id.b.al_pa = vha->d_id.b.al_pa;
  1507. vha->bidir_fcport.d_id.b.area = vha->d_id.b.area;
  1508. vha->bidir_fcport.d_id.b.domain = vha->d_id.b.domain;
  1509. vha->bidir_fcport.loop_id = vha->loop_id;
  1510. if (qla2x00_fabric_login(vha, &(vha->bidir_fcport), &nextlid)) {
  1511. ql_log(ql_log_warn, vha, 0x70a7,
  1512. "Failed to login port %06X for bidirectional IOCB\n",
  1513. vha->bidir_fcport.d_id.b24);
  1514. mutex_unlock(&ha->selflogin_lock);
  1515. rval = EXT_STATUS_MAILBOX;
  1516. goto done;
  1517. }
  1518. vha->self_login_loop_id = nextlid - 1;
  1519. }
  1520. /* Assign the self login loop id to fcport */
  1521. mutex_unlock(&ha->selflogin_lock);
  1522. vha->bidir_fcport.loop_id = vha->self_login_loop_id;
  1523. req_sg_cnt = dma_map_sg(&ha->pdev->dev,
  1524. bsg_job->request_payload.sg_list,
  1525. bsg_job->request_payload.sg_cnt,
  1526. DMA_TO_DEVICE);
  1527. if (!req_sg_cnt) {
  1528. rval = EXT_STATUS_NO_MEMORY;
  1529. goto done;
  1530. }
  1531. rsp_sg_cnt = dma_map_sg(&ha->pdev->dev,
  1532. bsg_job->reply_payload.sg_list, bsg_job->reply_payload.sg_cnt,
  1533. DMA_FROM_DEVICE);
  1534. if (!rsp_sg_cnt) {
  1535. rval = EXT_STATUS_NO_MEMORY;
  1536. goto done_unmap_req_sg;
  1537. }
  1538. if ((req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
  1539. (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
  1540. ql_dbg(ql_dbg_user, vha, 0x70a9,
  1541. "Dma mapping resulted in different sg counts "
  1542. "[request_sg_cnt: %x dma_request_sg_cnt: %x reply_sg_cnt: "
  1543. "%x dma_reply_sg_cnt: %x]\n",
  1544. bsg_job->request_payload.sg_cnt, req_sg_cnt,
  1545. bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
  1546. rval = EXT_STATUS_NO_MEMORY;
  1547. goto done_unmap_sg;
  1548. }
  1549. if (req_data_len != rsp_data_len) {
  1550. rval = EXT_STATUS_BUSY;
  1551. ql_log(ql_log_warn, vha, 0x70aa,
  1552. "req_data_len != rsp_data_len\n");
  1553. goto done_unmap_sg;
  1554. }
  1555. req_data_len = bsg_job->request_payload.payload_len;
  1556. rsp_data_len = bsg_job->reply_payload.payload_len;
  1557. /* Alloc SRB structure */
  1558. sp = qla2x00_get_sp(vha, &(vha->bidir_fcport), GFP_KERNEL);
  1559. if (!sp) {
  1560. ql_dbg(ql_dbg_user, vha, 0x70ac,
  1561. "Alloc SRB structure failed\n");
  1562. rval = EXT_STATUS_NO_MEMORY;
  1563. goto done_unmap_sg;
  1564. }
  1565. /*Populate srb->ctx with bidir ctx*/
  1566. sp->u.bsg_job = bsg_job;
  1567. sp->free = qla2x00_bsg_sp_free;
  1568. sp->type = SRB_BIDI_CMD;
  1569. sp->done = qla2x00_bsg_job_done;
  1570. /* Add the read and write sg count */
  1571. tot_dsds = rsp_sg_cnt + req_sg_cnt;
  1572. rval = qla2x00_start_bidir(sp, vha, tot_dsds);
  1573. if (rval != EXT_STATUS_OK)
  1574. goto done_free_srb;
  1575. /* the bsg request will be completed in the interrupt handler */
  1576. return rval;
  1577. done_free_srb:
  1578. mempool_free(sp, ha->srb_mempool);
  1579. done_unmap_sg:
  1580. dma_unmap_sg(&ha->pdev->dev,
  1581. bsg_job->reply_payload.sg_list,
  1582. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  1583. done_unmap_req_sg:
  1584. dma_unmap_sg(&ha->pdev->dev,
  1585. bsg_job->request_payload.sg_list,
  1586. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  1587. done:
  1588. /* Return an error vendor specific response
  1589. * and complete the bsg request
  1590. */
  1591. bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = rval;
  1592. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  1593. bsg_job->reply->reply_payload_rcv_len = 0;
  1594. bsg_job->reply->result = (DID_OK) << 16;
  1595. bsg_job->job_done(bsg_job);
  1596. /* Always retrun success, vendor rsp carries correct status */
  1597. return 0;
  1598. }
  1599. static int
  1600. qla2x00_process_vendor_specific(struct fc_bsg_job *bsg_job)
  1601. {
  1602. switch (bsg_job->request->rqst_data.h_vendor.vendor_cmd[0]) {
  1603. case QL_VND_LOOPBACK:
  1604. return qla2x00_process_loopback(bsg_job);
  1605. case QL_VND_A84_RESET:
  1606. return qla84xx_reset(bsg_job);
  1607. case QL_VND_A84_UPDATE_FW:
  1608. return qla84xx_updatefw(bsg_job);
  1609. case QL_VND_A84_MGMT_CMD:
  1610. return qla84xx_mgmt_cmd(bsg_job);
  1611. case QL_VND_IIDMA:
  1612. return qla24xx_iidma(bsg_job);
  1613. case QL_VND_FCP_PRIO_CFG_CMD:
  1614. return qla24xx_proc_fcp_prio_cfg_cmd(bsg_job);
  1615. case QL_VND_READ_FLASH:
  1616. return qla2x00_read_optrom(bsg_job);
  1617. case QL_VND_UPDATE_FLASH:
  1618. return qla2x00_update_optrom(bsg_job);
  1619. case QL_VND_SET_FRU_VERSION:
  1620. return qla2x00_update_fru_versions(bsg_job);
  1621. case QL_VND_READ_FRU_STATUS:
  1622. return qla2x00_read_fru_status(bsg_job);
  1623. case QL_VND_WRITE_FRU_STATUS:
  1624. return qla2x00_write_fru_status(bsg_job);
  1625. case QL_VND_WRITE_I2C:
  1626. return qla2x00_write_i2c(bsg_job);
  1627. case QL_VND_READ_I2C:
  1628. return qla2x00_read_i2c(bsg_job);
  1629. case QL_VND_DIAG_IO_CMD:
  1630. return qla24xx_process_bidir_cmd(bsg_job);
  1631. default:
  1632. return -ENOSYS;
  1633. }
  1634. }
  1635. int
  1636. qla24xx_bsg_request(struct fc_bsg_job *bsg_job)
  1637. {
  1638. int ret = -EINVAL;
  1639. struct fc_rport *rport;
  1640. fc_port_t *fcport = NULL;
  1641. struct Scsi_Host *host;
  1642. scsi_qla_host_t *vha;
  1643. /* In case no data transferred. */
  1644. bsg_job->reply->reply_payload_rcv_len = 0;
  1645. if (bsg_job->request->msgcode == FC_BSG_RPT_ELS) {
  1646. rport = bsg_job->rport;
  1647. fcport = *(fc_port_t **) rport->dd_data;
  1648. host = rport_to_shost(rport);
  1649. vha = shost_priv(host);
  1650. } else {
  1651. host = bsg_job->shost;
  1652. vha = shost_priv(host);
  1653. }
  1654. if (qla2x00_reset_active(vha)) {
  1655. ql_dbg(ql_dbg_user, vha, 0x709f,
  1656. "BSG: ISP abort active/needed -- cmd=%d.\n",
  1657. bsg_job->request->msgcode);
  1658. return -EBUSY;
  1659. }
  1660. ql_dbg(ql_dbg_user, vha, 0x7000,
  1661. "Entered %s msgcode=0x%x.\n", __func__, bsg_job->request->msgcode);
  1662. switch (bsg_job->request->msgcode) {
  1663. case FC_BSG_RPT_ELS:
  1664. case FC_BSG_HST_ELS_NOLOGIN:
  1665. ret = qla2x00_process_els(bsg_job);
  1666. break;
  1667. case FC_BSG_HST_CT:
  1668. ret = qla2x00_process_ct(bsg_job);
  1669. break;
  1670. case FC_BSG_HST_VENDOR:
  1671. ret = qla2x00_process_vendor_specific(bsg_job);
  1672. break;
  1673. case FC_BSG_HST_ADD_RPORT:
  1674. case FC_BSG_HST_DEL_RPORT:
  1675. case FC_BSG_RPT_CT:
  1676. default:
  1677. ql_log(ql_log_warn, vha, 0x705a, "Unsupported BSG request.\n");
  1678. break;
  1679. }
  1680. return ret;
  1681. }
  1682. int
  1683. qla24xx_bsg_timeout(struct fc_bsg_job *bsg_job)
  1684. {
  1685. scsi_qla_host_t *vha = shost_priv(bsg_job->shost);
  1686. struct qla_hw_data *ha = vha->hw;
  1687. srb_t *sp;
  1688. int cnt, que;
  1689. unsigned long flags;
  1690. struct req_que *req;
  1691. /* find the bsg job from the active list of commands */
  1692. spin_lock_irqsave(&ha->hardware_lock, flags);
  1693. for (que = 0; que < ha->max_req_queues; que++) {
  1694. req = ha->req_q_map[que];
  1695. if (!req)
  1696. continue;
  1697. for (cnt = 1; cnt < req->num_outstanding_cmds; cnt++) {
  1698. sp = req->outstanding_cmds[cnt];
  1699. if (sp) {
  1700. if (((sp->type == SRB_CT_CMD) ||
  1701. (sp->type == SRB_ELS_CMD_HST))
  1702. && (sp->u.bsg_job == bsg_job)) {
  1703. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1704. if (ha->isp_ops->abort_command(sp)) {
  1705. ql_log(ql_log_warn, vha, 0x7089,
  1706. "mbx abort_command "
  1707. "failed.\n");
  1708. bsg_job->req->errors =
  1709. bsg_job->reply->result = -EIO;
  1710. } else {
  1711. ql_dbg(ql_dbg_user, vha, 0x708a,
  1712. "mbx abort_command "
  1713. "success.\n");
  1714. bsg_job->req->errors =
  1715. bsg_job->reply->result = 0;
  1716. }
  1717. spin_lock_irqsave(&ha->hardware_lock, flags);
  1718. goto done;
  1719. }
  1720. }
  1721. }
  1722. }
  1723. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1724. ql_log(ql_log_info, vha, 0x708b, "SRB not found to abort.\n");
  1725. bsg_job->req->errors = bsg_job->reply->result = -ENXIO;
  1726. return 0;
  1727. done:
  1728. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1729. if (bsg_job->request->msgcode == FC_BSG_HST_CT)
  1730. kfree(sp->fcport);
  1731. qla2x00_rel_sp(vha, sp);
  1732. return 0;
  1733. }