qla_bsg.c 53 KB

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