bfad_im.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349
  1. /*
  2. * Copyright (c) 2005-2014 Brocade Communications Systems, Inc.
  3. * Copyright (c) 2014- QLogic Corporation.
  4. * All rights reserved
  5. * www.qlogic.com
  6. *
  7. * Linux driver for QLogic BR-series Fibre Channel Host Bus Adapter.
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License (GPL) Version 2 as
  11. * published by the Free Software Foundation
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. */
  18. /*
  19. * bfad_im.c Linux driver IM module.
  20. */
  21. #include <linux/export.h>
  22. #include "bfad_drv.h"
  23. #include "bfad_im.h"
  24. #include "bfa_fcs.h"
  25. BFA_TRC_FILE(LDRV, IM);
  26. DEFINE_IDR(bfad_im_port_index);
  27. struct scsi_transport_template *bfad_im_scsi_transport_template;
  28. struct scsi_transport_template *bfad_im_scsi_vport_transport_template;
  29. static void bfad_im_itnim_work_handler(struct work_struct *work);
  30. static int bfad_im_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmnd);
  31. static int bfad_im_slave_alloc(struct scsi_device *sdev);
  32. static void bfad_im_fc_rport_add(struct bfad_im_port_s *im_port,
  33. struct bfad_itnim_s *itnim);
  34. void
  35. bfa_cb_ioim_done(void *drv, struct bfad_ioim_s *dio,
  36. enum bfi_ioim_status io_status, u8 scsi_status,
  37. int sns_len, u8 *sns_info, s32 residue)
  38. {
  39. struct scsi_cmnd *cmnd = (struct scsi_cmnd *)dio;
  40. struct bfad_s *bfad = drv;
  41. struct bfad_itnim_data_s *itnim_data;
  42. struct bfad_itnim_s *itnim;
  43. u8 host_status = DID_OK;
  44. switch (io_status) {
  45. case BFI_IOIM_STS_OK:
  46. bfa_trc(bfad, scsi_status);
  47. scsi_set_resid(cmnd, 0);
  48. if (sns_len > 0) {
  49. bfa_trc(bfad, sns_len);
  50. if (sns_len > SCSI_SENSE_BUFFERSIZE)
  51. sns_len = SCSI_SENSE_BUFFERSIZE;
  52. memcpy(cmnd->sense_buffer, sns_info, sns_len);
  53. }
  54. if (residue > 0) {
  55. bfa_trc(bfad, residue);
  56. scsi_set_resid(cmnd, residue);
  57. if (!sns_len && (scsi_status == SAM_STAT_GOOD) &&
  58. (scsi_bufflen(cmnd) - residue) <
  59. cmnd->underflow) {
  60. bfa_trc(bfad, 0);
  61. host_status = DID_ERROR;
  62. }
  63. }
  64. cmnd->result = ScsiResult(host_status, scsi_status);
  65. break;
  66. case BFI_IOIM_STS_TIMEDOUT:
  67. host_status = DID_TIME_OUT;
  68. cmnd->result = ScsiResult(host_status, 0);
  69. break;
  70. case BFI_IOIM_STS_PATHTOV:
  71. host_status = DID_TRANSPORT_DISRUPTED;
  72. cmnd->result = ScsiResult(host_status, 0);
  73. break;
  74. default:
  75. host_status = DID_ERROR;
  76. cmnd->result = ScsiResult(host_status, 0);
  77. }
  78. /* Unmap DMA, if host is NULL, it means a scsi passthru cmd */
  79. if (cmnd->device->host != NULL)
  80. scsi_dma_unmap(cmnd);
  81. cmnd->host_scribble = NULL;
  82. bfa_trc(bfad, cmnd->result);
  83. itnim_data = cmnd->device->hostdata;
  84. if (itnim_data) {
  85. itnim = itnim_data->itnim;
  86. if (!cmnd->result && itnim &&
  87. (bfa_lun_queue_depth > cmnd->device->queue_depth)) {
  88. /* Queue depth adjustment for good status completion */
  89. bfad_ramp_up_qdepth(itnim, cmnd->device);
  90. } else if (cmnd->result == SAM_STAT_TASK_SET_FULL && itnim) {
  91. /* qfull handling */
  92. bfad_handle_qfull(itnim, cmnd->device);
  93. }
  94. }
  95. cmnd->scsi_done(cmnd);
  96. }
  97. void
  98. bfa_cb_ioim_good_comp(void *drv, struct bfad_ioim_s *dio)
  99. {
  100. struct scsi_cmnd *cmnd = (struct scsi_cmnd *)dio;
  101. struct bfad_itnim_data_s *itnim_data;
  102. struct bfad_itnim_s *itnim;
  103. cmnd->result = ScsiResult(DID_OK, SCSI_STATUS_GOOD);
  104. /* Unmap DMA, if host is NULL, it means a scsi passthru cmd */
  105. if (cmnd->device->host != NULL)
  106. scsi_dma_unmap(cmnd);
  107. cmnd->host_scribble = NULL;
  108. /* Queue depth adjustment */
  109. if (bfa_lun_queue_depth > cmnd->device->queue_depth) {
  110. itnim_data = cmnd->device->hostdata;
  111. if (itnim_data) {
  112. itnim = itnim_data->itnim;
  113. if (itnim)
  114. bfad_ramp_up_qdepth(itnim, cmnd->device);
  115. }
  116. }
  117. cmnd->scsi_done(cmnd);
  118. }
  119. void
  120. bfa_cb_ioim_abort(void *drv, struct bfad_ioim_s *dio)
  121. {
  122. struct scsi_cmnd *cmnd = (struct scsi_cmnd *)dio;
  123. struct bfad_s *bfad = drv;
  124. cmnd->result = ScsiResult(DID_ERROR, 0);
  125. /* Unmap DMA, if host is NULL, it means a scsi passthru cmd */
  126. if (cmnd->device->host != NULL)
  127. scsi_dma_unmap(cmnd);
  128. bfa_trc(bfad, cmnd->result);
  129. cmnd->host_scribble = NULL;
  130. }
  131. void
  132. bfa_cb_tskim_done(void *bfad, struct bfad_tskim_s *dtsk,
  133. enum bfi_tskim_status tsk_status)
  134. {
  135. struct scsi_cmnd *cmnd = (struct scsi_cmnd *)dtsk;
  136. wait_queue_head_t *wq;
  137. cmnd->SCp.Status |= tsk_status << 1;
  138. set_bit(IO_DONE_BIT, (unsigned long *)&cmnd->SCp.Status);
  139. wq = (wait_queue_head_t *) cmnd->SCp.ptr;
  140. cmnd->SCp.ptr = NULL;
  141. if (wq)
  142. wake_up(wq);
  143. }
  144. /*
  145. * Scsi_Host_template SCSI host template
  146. */
  147. /*
  148. * Scsi_Host template entry, returns BFAD PCI info.
  149. */
  150. static const char *
  151. bfad_im_info(struct Scsi_Host *shost)
  152. {
  153. static char bfa_buf[256];
  154. struct bfad_im_port_s *im_port =
  155. (struct bfad_im_port_s *) shost->hostdata[0];
  156. struct bfad_s *bfad = im_port->bfad;
  157. memset(bfa_buf, 0, sizeof(bfa_buf));
  158. snprintf(bfa_buf, sizeof(bfa_buf),
  159. "QLogic BR-series FC/FCOE Adapter, hwpath: %s driver: %s",
  160. bfad->pci_name, BFAD_DRIVER_VERSION);
  161. return bfa_buf;
  162. }
  163. /*
  164. * Scsi_Host template entry, aborts the specified SCSI command.
  165. *
  166. * Returns: SUCCESS or FAILED.
  167. */
  168. static int
  169. bfad_im_abort_handler(struct scsi_cmnd *cmnd)
  170. {
  171. struct Scsi_Host *shost = cmnd->device->host;
  172. struct bfad_im_port_s *im_port =
  173. (struct bfad_im_port_s *) shost->hostdata[0];
  174. struct bfad_s *bfad = im_port->bfad;
  175. struct bfa_ioim_s *hal_io;
  176. unsigned long flags;
  177. u32 timeout;
  178. int rc = FAILED;
  179. spin_lock_irqsave(&bfad->bfad_lock, flags);
  180. hal_io = (struct bfa_ioim_s *) cmnd->host_scribble;
  181. if (!hal_io) {
  182. /* IO has been completed, return success */
  183. rc = SUCCESS;
  184. goto out;
  185. }
  186. if (hal_io->dio != (struct bfad_ioim_s *) cmnd) {
  187. rc = FAILED;
  188. goto out;
  189. }
  190. bfa_trc(bfad, hal_io->iotag);
  191. BFA_LOG(KERN_INFO, bfad, bfa_log_level,
  192. "scsi%d: abort cmnd %p iotag %x\n",
  193. im_port->shost->host_no, cmnd, hal_io->iotag);
  194. (void) bfa_ioim_abort(hal_io);
  195. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  196. /* Need to wait until the command get aborted */
  197. timeout = 10;
  198. while ((struct bfa_ioim_s *) cmnd->host_scribble == hal_io) {
  199. set_current_state(TASK_UNINTERRUPTIBLE);
  200. schedule_timeout(timeout);
  201. if (timeout < 4 * HZ)
  202. timeout *= 2;
  203. }
  204. cmnd->scsi_done(cmnd);
  205. bfa_trc(bfad, hal_io->iotag);
  206. BFA_LOG(KERN_INFO, bfad, bfa_log_level,
  207. "scsi%d: complete abort 0x%p iotag 0x%x\n",
  208. im_port->shost->host_no, cmnd, hal_io->iotag);
  209. return SUCCESS;
  210. out:
  211. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  212. return rc;
  213. }
  214. static bfa_status_t
  215. bfad_im_target_reset_send(struct bfad_s *bfad, struct scsi_cmnd *cmnd,
  216. struct bfad_itnim_s *itnim)
  217. {
  218. struct bfa_tskim_s *tskim;
  219. struct bfa_itnim_s *bfa_itnim;
  220. bfa_status_t rc = BFA_STATUS_OK;
  221. struct scsi_lun scsilun;
  222. tskim = bfa_tskim_alloc(&bfad->bfa, (struct bfad_tskim_s *) cmnd);
  223. if (!tskim) {
  224. BFA_LOG(KERN_ERR, bfad, bfa_log_level,
  225. "target reset, fail to allocate tskim\n");
  226. rc = BFA_STATUS_FAILED;
  227. goto out;
  228. }
  229. /*
  230. * Set host_scribble to NULL to avoid aborting a task command if
  231. * happens.
  232. */
  233. cmnd->host_scribble = NULL;
  234. cmnd->SCp.Status = 0;
  235. bfa_itnim = bfa_fcs_itnim_get_halitn(&itnim->fcs_itnim);
  236. /*
  237. * bfa_itnim can be NULL if the port gets disconnected and the bfa
  238. * and fcs layers have cleaned up their nexus with the targets and
  239. * the same has not been cleaned up by the shim
  240. */
  241. if (bfa_itnim == NULL) {
  242. bfa_tskim_free(tskim);
  243. BFA_LOG(KERN_ERR, bfad, bfa_log_level,
  244. "target reset, bfa_itnim is NULL\n");
  245. rc = BFA_STATUS_FAILED;
  246. goto out;
  247. }
  248. memset(&scsilun, 0, sizeof(scsilun));
  249. bfa_tskim_start(tskim, bfa_itnim, scsilun,
  250. FCP_TM_TARGET_RESET, BFAD_TARGET_RESET_TMO);
  251. out:
  252. return rc;
  253. }
  254. /*
  255. * Scsi_Host template entry, resets a LUN and abort its all commands.
  256. *
  257. * Returns: SUCCESS or FAILED.
  258. *
  259. */
  260. static int
  261. bfad_im_reset_lun_handler(struct scsi_cmnd *cmnd)
  262. {
  263. struct Scsi_Host *shost = cmnd->device->host;
  264. struct bfad_im_port_s *im_port =
  265. (struct bfad_im_port_s *) shost->hostdata[0];
  266. struct bfad_itnim_data_s *itnim_data = cmnd->device->hostdata;
  267. struct bfad_s *bfad = im_port->bfad;
  268. struct bfa_tskim_s *tskim;
  269. struct bfad_itnim_s *itnim;
  270. struct bfa_itnim_s *bfa_itnim;
  271. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  272. int rc = SUCCESS;
  273. unsigned long flags;
  274. enum bfi_tskim_status task_status;
  275. struct scsi_lun scsilun;
  276. spin_lock_irqsave(&bfad->bfad_lock, flags);
  277. itnim = itnim_data->itnim;
  278. if (!itnim) {
  279. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  280. rc = FAILED;
  281. goto out;
  282. }
  283. tskim = bfa_tskim_alloc(&bfad->bfa, (struct bfad_tskim_s *) cmnd);
  284. if (!tskim) {
  285. BFA_LOG(KERN_ERR, bfad, bfa_log_level,
  286. "LUN reset, fail to allocate tskim");
  287. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  288. rc = FAILED;
  289. goto out;
  290. }
  291. /*
  292. * Set host_scribble to NULL to avoid aborting a task command
  293. * if happens.
  294. */
  295. cmnd->host_scribble = NULL;
  296. cmnd->SCp.ptr = (char *)&wq;
  297. cmnd->SCp.Status = 0;
  298. bfa_itnim = bfa_fcs_itnim_get_halitn(&itnim->fcs_itnim);
  299. /*
  300. * bfa_itnim can be NULL if the port gets disconnected and the bfa
  301. * and fcs layers have cleaned up their nexus with the targets and
  302. * the same has not been cleaned up by the shim
  303. */
  304. if (bfa_itnim == NULL) {
  305. bfa_tskim_free(tskim);
  306. BFA_LOG(KERN_ERR, bfad, bfa_log_level,
  307. "lun reset, bfa_itnim is NULL\n");
  308. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  309. rc = FAILED;
  310. goto out;
  311. }
  312. int_to_scsilun(cmnd->device->lun, &scsilun);
  313. bfa_tskim_start(tskim, bfa_itnim, scsilun,
  314. FCP_TM_LUN_RESET, BFAD_LUN_RESET_TMO);
  315. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  316. wait_event(wq, test_bit(IO_DONE_BIT,
  317. (unsigned long *)&cmnd->SCp.Status));
  318. task_status = cmnd->SCp.Status >> 1;
  319. if (task_status != BFI_TSKIM_STS_OK) {
  320. BFA_LOG(KERN_ERR, bfad, bfa_log_level,
  321. "LUN reset failure, status: %d\n", task_status);
  322. rc = FAILED;
  323. }
  324. out:
  325. return rc;
  326. }
  327. /*
  328. * Scsi_Host template entry, resets the bus and abort all commands.
  329. */
  330. static int
  331. bfad_im_reset_bus_handler(struct scsi_cmnd *cmnd)
  332. {
  333. struct Scsi_Host *shost = cmnd->device->host;
  334. struct bfad_im_port_s *im_port =
  335. (struct bfad_im_port_s *) shost->hostdata[0];
  336. struct bfad_s *bfad = im_port->bfad;
  337. struct bfad_itnim_s *itnim;
  338. unsigned long flags;
  339. u32 i, rc, err_cnt = 0;
  340. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  341. enum bfi_tskim_status task_status;
  342. spin_lock_irqsave(&bfad->bfad_lock, flags);
  343. for (i = 0; i < MAX_FCP_TARGET; i++) {
  344. itnim = bfad_get_itnim(im_port, i);
  345. if (itnim) {
  346. cmnd->SCp.ptr = (char *)&wq;
  347. rc = bfad_im_target_reset_send(bfad, cmnd, itnim);
  348. if (rc != BFA_STATUS_OK) {
  349. err_cnt++;
  350. continue;
  351. }
  352. /* wait target reset to complete */
  353. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  354. wait_event(wq, test_bit(IO_DONE_BIT,
  355. (unsigned long *)&cmnd->SCp.Status));
  356. spin_lock_irqsave(&bfad->bfad_lock, flags);
  357. task_status = cmnd->SCp.Status >> 1;
  358. if (task_status != BFI_TSKIM_STS_OK) {
  359. BFA_LOG(KERN_ERR, bfad, bfa_log_level,
  360. "target reset failure,"
  361. " status: %d\n", task_status);
  362. err_cnt++;
  363. }
  364. }
  365. }
  366. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  367. if (err_cnt)
  368. return FAILED;
  369. return SUCCESS;
  370. }
  371. /*
  372. * Scsi_Host template entry slave_destroy.
  373. */
  374. static void
  375. bfad_im_slave_destroy(struct scsi_device *sdev)
  376. {
  377. sdev->hostdata = NULL;
  378. return;
  379. }
  380. /*
  381. * BFA FCS itnim callbacks
  382. */
  383. /*
  384. * BFA FCS itnim alloc callback, after successful PRLI
  385. * Context: Interrupt
  386. */
  387. int
  388. bfa_fcb_itnim_alloc(struct bfad_s *bfad, struct bfa_fcs_itnim_s **itnim,
  389. struct bfad_itnim_s **itnim_drv)
  390. {
  391. *itnim_drv = kzalloc(sizeof(struct bfad_itnim_s), GFP_ATOMIC);
  392. if (*itnim_drv == NULL)
  393. return -ENOMEM;
  394. (*itnim_drv)->im = bfad->im;
  395. *itnim = &(*itnim_drv)->fcs_itnim;
  396. (*itnim_drv)->state = ITNIM_STATE_NONE;
  397. /*
  398. * Initiaze the itnim_work
  399. */
  400. INIT_WORK(&(*itnim_drv)->itnim_work, bfad_im_itnim_work_handler);
  401. bfad->bfad_flags |= BFAD_RPORT_ONLINE;
  402. return 0;
  403. }
  404. /*
  405. * BFA FCS itnim free callback.
  406. * Context: Interrupt. bfad_lock is held
  407. */
  408. void
  409. bfa_fcb_itnim_free(struct bfad_s *bfad, struct bfad_itnim_s *itnim_drv)
  410. {
  411. struct bfad_port_s *port;
  412. wwn_t wwpn;
  413. u32 fcid;
  414. char wwpn_str[32], fcid_str[16];
  415. struct bfad_im_s *im = itnim_drv->im;
  416. /* online to free state transtion should not happen */
  417. WARN_ON(itnim_drv->state == ITNIM_STATE_ONLINE);
  418. itnim_drv->queue_work = 1;
  419. /* offline request is not yet done, use the same request to free */
  420. if (itnim_drv->state == ITNIM_STATE_OFFLINE_PENDING)
  421. itnim_drv->queue_work = 0;
  422. itnim_drv->state = ITNIM_STATE_FREE;
  423. port = bfa_fcs_itnim_get_drvport(&itnim_drv->fcs_itnim);
  424. itnim_drv->im_port = port->im_port;
  425. wwpn = bfa_fcs_itnim_get_pwwn(&itnim_drv->fcs_itnim);
  426. fcid = bfa_fcs_itnim_get_fcid(&itnim_drv->fcs_itnim);
  427. wwn2str(wwpn_str, wwpn);
  428. fcid2str(fcid_str, fcid);
  429. BFA_LOG(KERN_INFO, bfad, bfa_log_level,
  430. "ITNIM FREE scsi%d: FCID: %s WWPN: %s\n",
  431. port->im_port->shost->host_no,
  432. fcid_str, wwpn_str);
  433. /* ITNIM processing */
  434. if (itnim_drv->queue_work)
  435. queue_work(im->drv_workq, &itnim_drv->itnim_work);
  436. }
  437. /*
  438. * BFA FCS itnim online callback.
  439. * Context: Interrupt. bfad_lock is held
  440. */
  441. void
  442. bfa_fcb_itnim_online(struct bfad_itnim_s *itnim_drv)
  443. {
  444. struct bfad_port_s *port;
  445. struct bfad_im_s *im = itnim_drv->im;
  446. itnim_drv->bfa_itnim = bfa_fcs_itnim_get_halitn(&itnim_drv->fcs_itnim);
  447. port = bfa_fcs_itnim_get_drvport(&itnim_drv->fcs_itnim);
  448. itnim_drv->state = ITNIM_STATE_ONLINE;
  449. itnim_drv->queue_work = 1;
  450. itnim_drv->im_port = port->im_port;
  451. /* ITNIM processing */
  452. if (itnim_drv->queue_work)
  453. queue_work(im->drv_workq, &itnim_drv->itnim_work);
  454. }
  455. /*
  456. * BFA FCS itnim offline callback.
  457. * Context: Interrupt. bfad_lock is held
  458. */
  459. void
  460. bfa_fcb_itnim_offline(struct bfad_itnim_s *itnim_drv)
  461. {
  462. struct bfad_port_s *port;
  463. struct bfad_s *bfad;
  464. struct bfad_im_s *im = itnim_drv->im;
  465. port = bfa_fcs_itnim_get_drvport(&itnim_drv->fcs_itnim);
  466. bfad = port->bfad;
  467. if ((bfad->pport.flags & BFAD_PORT_DELETE) ||
  468. (port->flags & BFAD_PORT_DELETE)) {
  469. itnim_drv->state = ITNIM_STATE_OFFLINE;
  470. return;
  471. }
  472. itnim_drv->im_port = port->im_port;
  473. itnim_drv->state = ITNIM_STATE_OFFLINE_PENDING;
  474. itnim_drv->queue_work = 1;
  475. /* ITNIM processing */
  476. if (itnim_drv->queue_work)
  477. queue_work(im->drv_workq, &itnim_drv->itnim_work);
  478. }
  479. /*
  480. * Allocate a Scsi_Host for a port.
  481. */
  482. int
  483. bfad_im_scsi_host_alloc(struct bfad_s *bfad, struct bfad_im_port_s *im_port,
  484. struct device *dev)
  485. {
  486. int error = 1;
  487. mutex_lock(&bfad_mutex);
  488. error = idr_alloc(&bfad_im_port_index, im_port, 0, 0, GFP_KERNEL);
  489. if (error < 0) {
  490. mutex_unlock(&bfad_mutex);
  491. printk(KERN_WARNING "idr_alloc failure\n");
  492. goto out;
  493. }
  494. im_port->idr_id = error;
  495. mutex_unlock(&bfad_mutex);
  496. im_port->shost = bfad_scsi_host_alloc(im_port, bfad);
  497. if (!im_port->shost) {
  498. error = 1;
  499. goto out_free_idr;
  500. }
  501. im_port->shost->hostdata[0] = (unsigned long)im_port;
  502. im_port->shost->unique_id = im_port->idr_id;
  503. im_port->shost->this_id = -1;
  504. im_port->shost->max_id = MAX_FCP_TARGET;
  505. im_port->shost->max_lun = MAX_FCP_LUN;
  506. im_port->shost->max_cmd_len = 16;
  507. im_port->shost->can_queue = bfad->cfg_data.ioc_queue_depth;
  508. if (im_port->port->pvb_type == BFAD_PORT_PHYS_BASE)
  509. im_port->shost->transportt = bfad_im_scsi_transport_template;
  510. else
  511. im_port->shost->transportt =
  512. bfad_im_scsi_vport_transport_template;
  513. error = scsi_add_host_with_dma(im_port->shost, dev, &bfad->pcidev->dev);
  514. if (error) {
  515. printk(KERN_WARNING "scsi_add_host failure %d\n", error);
  516. goto out_fc_rel;
  517. }
  518. return 0;
  519. out_fc_rel:
  520. scsi_host_put(im_port->shost);
  521. im_port->shost = NULL;
  522. out_free_idr:
  523. mutex_lock(&bfad_mutex);
  524. idr_remove(&bfad_im_port_index, im_port->idr_id);
  525. mutex_unlock(&bfad_mutex);
  526. out:
  527. return error;
  528. }
  529. void
  530. bfad_im_scsi_host_free(struct bfad_s *bfad, struct bfad_im_port_s *im_port)
  531. {
  532. bfa_trc(bfad, bfad->inst_no);
  533. BFA_LOG(KERN_INFO, bfad, bfa_log_level, "Free scsi%d\n",
  534. im_port->shost->host_no);
  535. fc_remove_host(im_port->shost);
  536. scsi_remove_host(im_port->shost);
  537. scsi_host_put(im_port->shost);
  538. mutex_lock(&bfad_mutex);
  539. idr_remove(&bfad_im_port_index, im_port->idr_id);
  540. mutex_unlock(&bfad_mutex);
  541. }
  542. static void
  543. bfad_im_port_delete_handler(struct work_struct *work)
  544. {
  545. struct bfad_im_port_s *im_port =
  546. container_of(work, struct bfad_im_port_s, port_delete_work);
  547. if (im_port->port->pvb_type != BFAD_PORT_PHYS_BASE) {
  548. im_port->flags |= BFAD_PORT_DELETE;
  549. fc_vport_terminate(im_port->fc_vport);
  550. }
  551. }
  552. bfa_status_t
  553. bfad_im_port_new(struct bfad_s *bfad, struct bfad_port_s *port)
  554. {
  555. int rc = BFA_STATUS_OK;
  556. struct bfad_im_port_s *im_port;
  557. im_port = kzalloc(sizeof(struct bfad_im_port_s), GFP_ATOMIC);
  558. if (im_port == NULL) {
  559. rc = BFA_STATUS_ENOMEM;
  560. goto ext;
  561. }
  562. port->im_port = im_port;
  563. im_port->port = port;
  564. im_port->bfad = bfad;
  565. INIT_WORK(&im_port->port_delete_work, bfad_im_port_delete_handler);
  566. INIT_LIST_HEAD(&im_port->itnim_mapped_list);
  567. INIT_LIST_HEAD(&im_port->binding_list);
  568. ext:
  569. return rc;
  570. }
  571. void
  572. bfad_im_port_delete(struct bfad_s *bfad, struct bfad_port_s *port)
  573. {
  574. struct bfad_im_port_s *im_port = port->im_port;
  575. queue_work(bfad->im->drv_workq,
  576. &im_port->port_delete_work);
  577. }
  578. void
  579. bfad_im_port_clean(struct bfad_im_port_s *im_port)
  580. {
  581. struct bfad_fcp_binding *bp, *bp_new;
  582. unsigned long flags;
  583. struct bfad_s *bfad = im_port->bfad;
  584. spin_lock_irqsave(&bfad->bfad_lock, flags);
  585. list_for_each_entry_safe(bp, bp_new, &im_port->binding_list,
  586. list_entry) {
  587. list_del(&bp->list_entry);
  588. kfree(bp);
  589. }
  590. /* the itnim_mapped_list must be empty at this time */
  591. WARN_ON(!list_empty(&im_port->itnim_mapped_list));
  592. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  593. }
  594. static void bfad_aen_im_notify_handler(struct work_struct *work)
  595. {
  596. struct bfad_im_s *im =
  597. container_of(work, struct bfad_im_s, aen_im_notify_work);
  598. struct bfa_aen_entry_s *aen_entry;
  599. struct bfad_s *bfad = im->bfad;
  600. struct Scsi_Host *shost = bfad->pport.im_port->shost;
  601. void *event_data;
  602. unsigned long flags;
  603. while (!list_empty(&bfad->active_aen_q)) {
  604. spin_lock_irqsave(&bfad->bfad_aen_spinlock, flags);
  605. bfa_q_deq(&bfad->active_aen_q, &aen_entry);
  606. spin_unlock_irqrestore(&bfad->bfad_aen_spinlock, flags);
  607. event_data = (char *)aen_entry + sizeof(struct list_head);
  608. fc_host_post_vendor_event(shost, fc_get_event_number(),
  609. sizeof(struct bfa_aen_entry_s) -
  610. sizeof(struct list_head),
  611. (char *)event_data, BFAD_NL_VENDOR_ID);
  612. spin_lock_irqsave(&bfad->bfad_aen_spinlock, flags);
  613. list_add_tail(&aen_entry->qe, &bfad->free_aen_q);
  614. spin_unlock_irqrestore(&bfad->bfad_aen_spinlock, flags);
  615. }
  616. }
  617. bfa_status_t
  618. bfad_im_probe(struct bfad_s *bfad)
  619. {
  620. struct bfad_im_s *im;
  621. im = kzalloc(sizeof(struct bfad_im_s), GFP_KERNEL);
  622. if (im == NULL)
  623. return BFA_STATUS_ENOMEM;
  624. bfad->im = im;
  625. im->bfad = bfad;
  626. if (bfad_thread_workq(bfad) != BFA_STATUS_OK) {
  627. kfree(im);
  628. return BFA_STATUS_FAILED;
  629. }
  630. INIT_WORK(&im->aen_im_notify_work, bfad_aen_im_notify_handler);
  631. return BFA_STATUS_OK;
  632. }
  633. void
  634. bfad_im_probe_undo(struct bfad_s *bfad)
  635. {
  636. if (bfad->im) {
  637. bfad_destroy_workq(bfad->im);
  638. kfree(bfad->im);
  639. bfad->im = NULL;
  640. }
  641. }
  642. struct Scsi_Host *
  643. bfad_scsi_host_alloc(struct bfad_im_port_s *im_port, struct bfad_s *bfad)
  644. {
  645. struct scsi_host_template *sht;
  646. if (im_port->port->pvb_type == BFAD_PORT_PHYS_BASE)
  647. sht = &bfad_im_scsi_host_template;
  648. else
  649. sht = &bfad_im_vport_template;
  650. if (max_xfer_size != BFAD_MAX_SECTORS >> 1)
  651. sht->max_sectors = max_xfer_size << 1;
  652. sht->sg_tablesize = bfad->cfg_data.io_max_sge;
  653. return scsi_host_alloc(sht, sizeof(unsigned long));
  654. }
  655. void
  656. bfad_scsi_host_free(struct bfad_s *bfad, struct bfad_im_port_s *im_port)
  657. {
  658. if (!(im_port->flags & BFAD_PORT_DELETE))
  659. flush_workqueue(bfad->im->drv_workq);
  660. bfad_im_scsi_host_free(im_port->bfad, im_port);
  661. bfad_im_port_clean(im_port);
  662. kfree(im_port);
  663. }
  664. void
  665. bfad_destroy_workq(struct bfad_im_s *im)
  666. {
  667. if (im && im->drv_workq) {
  668. flush_workqueue(im->drv_workq);
  669. destroy_workqueue(im->drv_workq);
  670. im->drv_workq = NULL;
  671. }
  672. }
  673. bfa_status_t
  674. bfad_thread_workq(struct bfad_s *bfad)
  675. {
  676. struct bfad_im_s *im = bfad->im;
  677. bfa_trc(bfad, 0);
  678. snprintf(im->drv_workq_name, KOBJ_NAME_LEN, "bfad_wq_%d",
  679. bfad->inst_no);
  680. im->drv_workq = create_singlethread_workqueue(im->drv_workq_name);
  681. if (!im->drv_workq)
  682. return BFA_STATUS_FAILED;
  683. return BFA_STATUS_OK;
  684. }
  685. /*
  686. * Scsi_Host template entry.
  687. *
  688. * Description:
  689. * OS entry point to adjust the queue_depths on a per-device basis.
  690. * Called once per device during the bus scan.
  691. * Return non-zero if fails.
  692. */
  693. static int
  694. bfad_im_slave_configure(struct scsi_device *sdev)
  695. {
  696. scsi_change_queue_depth(sdev, bfa_lun_queue_depth);
  697. return 0;
  698. }
  699. struct scsi_host_template bfad_im_scsi_host_template = {
  700. .module = THIS_MODULE,
  701. .name = BFAD_DRIVER_NAME,
  702. .info = bfad_im_info,
  703. .queuecommand = bfad_im_queuecommand,
  704. .eh_abort_handler = bfad_im_abort_handler,
  705. .eh_device_reset_handler = bfad_im_reset_lun_handler,
  706. .eh_bus_reset_handler = bfad_im_reset_bus_handler,
  707. .slave_alloc = bfad_im_slave_alloc,
  708. .slave_configure = bfad_im_slave_configure,
  709. .slave_destroy = bfad_im_slave_destroy,
  710. .this_id = -1,
  711. .sg_tablesize = BFAD_IO_MAX_SGE,
  712. .cmd_per_lun = 3,
  713. .use_clustering = ENABLE_CLUSTERING,
  714. .shost_attrs = bfad_im_host_attrs,
  715. .max_sectors = BFAD_MAX_SECTORS,
  716. .vendor_id = BFA_PCI_VENDOR_ID_BROCADE,
  717. };
  718. struct scsi_host_template bfad_im_vport_template = {
  719. .module = THIS_MODULE,
  720. .name = BFAD_DRIVER_NAME,
  721. .info = bfad_im_info,
  722. .queuecommand = bfad_im_queuecommand,
  723. .eh_abort_handler = bfad_im_abort_handler,
  724. .eh_device_reset_handler = bfad_im_reset_lun_handler,
  725. .eh_bus_reset_handler = bfad_im_reset_bus_handler,
  726. .slave_alloc = bfad_im_slave_alloc,
  727. .slave_configure = bfad_im_slave_configure,
  728. .slave_destroy = bfad_im_slave_destroy,
  729. .this_id = -1,
  730. .sg_tablesize = BFAD_IO_MAX_SGE,
  731. .cmd_per_lun = 3,
  732. .use_clustering = ENABLE_CLUSTERING,
  733. .shost_attrs = bfad_im_vport_attrs,
  734. .max_sectors = BFAD_MAX_SECTORS,
  735. };
  736. bfa_status_t
  737. bfad_im_module_init(void)
  738. {
  739. bfad_im_scsi_transport_template =
  740. fc_attach_transport(&bfad_im_fc_function_template);
  741. if (!bfad_im_scsi_transport_template)
  742. return BFA_STATUS_ENOMEM;
  743. bfad_im_scsi_vport_transport_template =
  744. fc_attach_transport(&bfad_im_vport_fc_function_template);
  745. if (!bfad_im_scsi_vport_transport_template) {
  746. fc_release_transport(bfad_im_scsi_transport_template);
  747. return BFA_STATUS_ENOMEM;
  748. }
  749. return BFA_STATUS_OK;
  750. }
  751. void
  752. bfad_im_module_exit(void)
  753. {
  754. if (bfad_im_scsi_transport_template)
  755. fc_release_transport(bfad_im_scsi_transport_template);
  756. if (bfad_im_scsi_vport_transport_template)
  757. fc_release_transport(bfad_im_scsi_vport_transport_template);
  758. idr_destroy(&bfad_im_port_index);
  759. }
  760. void
  761. bfad_ramp_up_qdepth(struct bfad_itnim_s *itnim, struct scsi_device *sdev)
  762. {
  763. struct scsi_device *tmp_sdev;
  764. if (((jiffies - itnim->last_ramp_up_time) >
  765. BFA_QUEUE_FULL_RAMP_UP_TIME * HZ) &&
  766. ((jiffies - itnim->last_queue_full_time) >
  767. BFA_QUEUE_FULL_RAMP_UP_TIME * HZ)) {
  768. shost_for_each_device(tmp_sdev, sdev->host) {
  769. if (bfa_lun_queue_depth > tmp_sdev->queue_depth) {
  770. if (tmp_sdev->id != sdev->id)
  771. continue;
  772. scsi_change_queue_depth(tmp_sdev,
  773. tmp_sdev->queue_depth + 1);
  774. itnim->last_ramp_up_time = jiffies;
  775. }
  776. }
  777. }
  778. }
  779. void
  780. bfad_handle_qfull(struct bfad_itnim_s *itnim, struct scsi_device *sdev)
  781. {
  782. struct scsi_device *tmp_sdev;
  783. itnim->last_queue_full_time = jiffies;
  784. shost_for_each_device(tmp_sdev, sdev->host) {
  785. if (tmp_sdev->id != sdev->id)
  786. continue;
  787. scsi_track_queue_full(tmp_sdev, tmp_sdev->queue_depth - 1);
  788. }
  789. }
  790. struct bfad_itnim_s *
  791. bfad_get_itnim(struct bfad_im_port_s *im_port, int id)
  792. {
  793. struct bfad_itnim_s *itnim = NULL;
  794. /* Search the mapped list for this target ID */
  795. list_for_each_entry(itnim, &im_port->itnim_mapped_list, list_entry) {
  796. if (id == itnim->scsi_tgt_id)
  797. return itnim;
  798. }
  799. return NULL;
  800. }
  801. /*
  802. * Function is invoked from the SCSI Host Template slave_alloc() entry point.
  803. * Has the logic to query the LUN Mask database to check if this LUN needs to
  804. * be made visible to the SCSI mid-layer or not.
  805. *
  806. * Returns BFA_STATUS_OK if this LUN needs to be added to the OS stack.
  807. * Returns -ENXIO to notify SCSI mid-layer to not add this LUN to the OS stack.
  808. */
  809. static int
  810. bfad_im_check_if_make_lun_visible(struct scsi_device *sdev,
  811. struct fc_rport *rport)
  812. {
  813. struct bfad_itnim_data_s *itnim_data =
  814. (struct bfad_itnim_data_s *) rport->dd_data;
  815. struct bfa_s *bfa = itnim_data->itnim->bfa_itnim->bfa;
  816. struct bfa_rport_s *bfa_rport = itnim_data->itnim->bfa_itnim->rport;
  817. struct bfa_lun_mask_s *lun_list = bfa_get_lun_mask_list(bfa);
  818. int i = 0, ret = -ENXIO;
  819. for (i = 0; i < MAX_LUN_MASK_CFG; i++) {
  820. if (lun_list[i].state == BFA_IOIM_LUN_MASK_ACTIVE &&
  821. scsilun_to_int(&lun_list[i].lun) == sdev->lun &&
  822. lun_list[i].rp_tag == bfa_rport->rport_tag &&
  823. lun_list[i].lp_tag == (u8)bfa_rport->rport_info.lp_tag) {
  824. ret = BFA_STATUS_OK;
  825. break;
  826. }
  827. }
  828. return ret;
  829. }
  830. /*
  831. * Scsi_Host template entry slave_alloc
  832. */
  833. static int
  834. bfad_im_slave_alloc(struct scsi_device *sdev)
  835. {
  836. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  837. struct bfad_itnim_data_s *itnim_data;
  838. struct bfa_s *bfa;
  839. if (!rport || fc_remote_port_chkready(rport))
  840. return -ENXIO;
  841. itnim_data = (struct bfad_itnim_data_s *) rport->dd_data;
  842. bfa = itnim_data->itnim->bfa_itnim->bfa;
  843. if (bfa_get_lun_mask_status(bfa) == BFA_LUNMASK_ENABLED) {
  844. /*
  845. * We should not mask LUN 0 - since this will translate
  846. * to no LUN / TARGET for SCSI ml resulting no scan.
  847. */
  848. if (sdev->lun == 0) {
  849. sdev->sdev_bflags |= BLIST_NOREPORTLUN |
  850. BLIST_SPARSELUN;
  851. goto done;
  852. }
  853. /*
  854. * Query LUN Mask configuration - to expose this LUN
  855. * to the SCSI mid-layer or to mask it.
  856. */
  857. if (bfad_im_check_if_make_lun_visible(sdev, rport) !=
  858. BFA_STATUS_OK)
  859. return -ENXIO;
  860. }
  861. done:
  862. sdev->hostdata = rport->dd_data;
  863. return 0;
  864. }
  865. u32
  866. bfad_im_supported_speeds(struct bfa_s *bfa)
  867. {
  868. struct bfa_ioc_attr_s *ioc_attr;
  869. u32 supported_speed = 0;
  870. ioc_attr = kzalloc(sizeof(struct bfa_ioc_attr_s), GFP_KERNEL);
  871. if (!ioc_attr)
  872. return 0;
  873. bfa_ioc_get_attr(&bfa->ioc, ioc_attr);
  874. if (ioc_attr->adapter_attr.max_speed == BFA_PORT_SPEED_16GBPS)
  875. supported_speed |= FC_PORTSPEED_16GBIT | FC_PORTSPEED_8GBIT |
  876. FC_PORTSPEED_4GBIT | FC_PORTSPEED_2GBIT;
  877. else if (ioc_attr->adapter_attr.max_speed == BFA_PORT_SPEED_8GBPS) {
  878. if (ioc_attr->adapter_attr.is_mezz) {
  879. supported_speed |= FC_PORTSPEED_8GBIT |
  880. FC_PORTSPEED_4GBIT |
  881. FC_PORTSPEED_2GBIT | FC_PORTSPEED_1GBIT;
  882. } else {
  883. supported_speed |= FC_PORTSPEED_8GBIT |
  884. FC_PORTSPEED_4GBIT |
  885. FC_PORTSPEED_2GBIT;
  886. }
  887. } else if (ioc_attr->adapter_attr.max_speed == BFA_PORT_SPEED_4GBPS) {
  888. supported_speed |= FC_PORTSPEED_4GBIT | FC_PORTSPEED_2GBIT |
  889. FC_PORTSPEED_1GBIT;
  890. } else if (ioc_attr->adapter_attr.max_speed == BFA_PORT_SPEED_10GBPS) {
  891. supported_speed |= FC_PORTSPEED_10GBIT;
  892. }
  893. kfree(ioc_attr);
  894. return supported_speed;
  895. }
  896. void
  897. bfad_fc_host_init(struct bfad_im_port_s *im_port)
  898. {
  899. struct Scsi_Host *host = im_port->shost;
  900. struct bfad_s *bfad = im_port->bfad;
  901. struct bfad_port_s *port = im_port->port;
  902. char symname[BFA_SYMNAME_MAXLEN];
  903. struct bfa_fcport_s *fcport = BFA_FCPORT_MOD(&bfad->bfa);
  904. fc_host_node_name(host) =
  905. cpu_to_be64((bfa_fcs_lport_get_nwwn(port->fcs_port)));
  906. fc_host_port_name(host) =
  907. cpu_to_be64((bfa_fcs_lport_get_pwwn(port->fcs_port)));
  908. fc_host_max_npiv_vports(host) = bfa_lps_get_max_vport(&bfad->bfa);
  909. fc_host_supported_classes(host) = FC_COS_CLASS3;
  910. memset(fc_host_supported_fc4s(host), 0,
  911. sizeof(fc_host_supported_fc4s(host)));
  912. if (supported_fc4s & BFA_LPORT_ROLE_FCP_IM)
  913. /* For FCP type 0x08 */
  914. fc_host_supported_fc4s(host)[2] = 1;
  915. /* For fibre channel services type 0x20 */
  916. fc_host_supported_fc4s(host)[7] = 1;
  917. strlcpy(symname, bfad->bfa_fcs.fabric.bport.port_cfg.sym_name.symname,
  918. BFA_SYMNAME_MAXLEN);
  919. sprintf(fc_host_symbolic_name(host), "%s", symname);
  920. fc_host_supported_speeds(host) = bfad_im_supported_speeds(&bfad->bfa);
  921. fc_host_maxframe_size(host) = fcport->cfg.maxfrsize;
  922. }
  923. static void
  924. bfad_im_fc_rport_add(struct bfad_im_port_s *im_port, struct bfad_itnim_s *itnim)
  925. {
  926. struct fc_rport_identifiers rport_ids;
  927. struct fc_rport *fc_rport;
  928. struct bfad_itnim_data_s *itnim_data;
  929. rport_ids.node_name =
  930. cpu_to_be64(bfa_fcs_itnim_get_nwwn(&itnim->fcs_itnim));
  931. rport_ids.port_name =
  932. cpu_to_be64(bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim));
  933. rport_ids.port_id =
  934. bfa_hton3b(bfa_fcs_itnim_get_fcid(&itnim->fcs_itnim));
  935. rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
  936. itnim->fc_rport = fc_rport =
  937. fc_remote_port_add(im_port->shost, 0, &rport_ids);
  938. if (!fc_rport)
  939. return;
  940. fc_rport->maxframe_size =
  941. bfa_fcs_itnim_get_maxfrsize(&itnim->fcs_itnim);
  942. fc_rport->supported_classes = bfa_fcs_itnim_get_cos(&itnim->fcs_itnim);
  943. itnim_data = fc_rport->dd_data;
  944. itnim_data->itnim = itnim;
  945. rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
  946. if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
  947. fc_remote_port_rolechg(fc_rport, rport_ids.roles);
  948. if ((fc_rport->scsi_target_id != -1)
  949. && (fc_rport->scsi_target_id < MAX_FCP_TARGET))
  950. itnim->scsi_tgt_id = fc_rport->scsi_target_id;
  951. itnim->channel = fc_rport->channel;
  952. return;
  953. }
  954. /*
  955. * Work queue handler using FC transport service
  956. * Context: kernel
  957. */
  958. static void
  959. bfad_im_itnim_work_handler(struct work_struct *work)
  960. {
  961. struct bfad_itnim_s *itnim = container_of(work, struct bfad_itnim_s,
  962. itnim_work);
  963. struct bfad_im_s *im = itnim->im;
  964. struct bfad_s *bfad = im->bfad;
  965. struct bfad_im_port_s *im_port;
  966. unsigned long flags;
  967. struct fc_rport *fc_rport;
  968. wwn_t wwpn;
  969. u32 fcid;
  970. char wwpn_str[32], fcid_str[16];
  971. spin_lock_irqsave(&bfad->bfad_lock, flags);
  972. im_port = itnim->im_port;
  973. bfa_trc(bfad, itnim->state);
  974. switch (itnim->state) {
  975. case ITNIM_STATE_ONLINE:
  976. if (!itnim->fc_rport) {
  977. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  978. bfad_im_fc_rport_add(im_port, itnim);
  979. spin_lock_irqsave(&bfad->bfad_lock, flags);
  980. wwpn = bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim);
  981. fcid = bfa_fcs_itnim_get_fcid(&itnim->fcs_itnim);
  982. wwn2str(wwpn_str, wwpn);
  983. fcid2str(fcid_str, fcid);
  984. list_add_tail(&itnim->list_entry,
  985. &im_port->itnim_mapped_list);
  986. BFA_LOG(KERN_INFO, bfad, bfa_log_level,
  987. "ITNIM ONLINE Target: %d:0:%d "
  988. "FCID: %s WWPN: %s\n",
  989. im_port->shost->host_no,
  990. itnim->scsi_tgt_id,
  991. fcid_str, wwpn_str);
  992. } else {
  993. printk(KERN_WARNING
  994. "%s: itnim %llx is already in online state\n",
  995. __func__,
  996. bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim));
  997. }
  998. break;
  999. case ITNIM_STATE_OFFLINE_PENDING:
  1000. itnim->state = ITNIM_STATE_OFFLINE;
  1001. if (itnim->fc_rport) {
  1002. fc_rport = itnim->fc_rport;
  1003. ((struct bfad_itnim_data_s *)
  1004. fc_rport->dd_data)->itnim = NULL;
  1005. itnim->fc_rport = NULL;
  1006. if (!(im_port->port->flags & BFAD_PORT_DELETE)) {
  1007. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  1008. fc_rport->dev_loss_tmo =
  1009. bfa_fcpim_path_tov_get(&bfad->bfa) + 1;
  1010. fc_remote_port_delete(fc_rport);
  1011. spin_lock_irqsave(&bfad->bfad_lock, flags);
  1012. }
  1013. wwpn = bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim);
  1014. fcid = bfa_fcs_itnim_get_fcid(&itnim->fcs_itnim);
  1015. wwn2str(wwpn_str, wwpn);
  1016. fcid2str(fcid_str, fcid);
  1017. list_del(&itnim->list_entry);
  1018. BFA_LOG(KERN_INFO, bfad, bfa_log_level,
  1019. "ITNIM OFFLINE Target: %d:0:%d "
  1020. "FCID: %s WWPN: %s\n",
  1021. im_port->shost->host_no,
  1022. itnim->scsi_tgt_id,
  1023. fcid_str, wwpn_str);
  1024. }
  1025. break;
  1026. case ITNIM_STATE_FREE:
  1027. if (itnim->fc_rport) {
  1028. fc_rport = itnim->fc_rport;
  1029. ((struct bfad_itnim_data_s *)
  1030. fc_rport->dd_data)->itnim = NULL;
  1031. itnim->fc_rport = NULL;
  1032. if (!(im_port->port->flags & BFAD_PORT_DELETE)) {
  1033. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  1034. fc_rport->dev_loss_tmo =
  1035. bfa_fcpim_path_tov_get(&bfad->bfa) + 1;
  1036. fc_remote_port_delete(fc_rport);
  1037. spin_lock_irqsave(&bfad->bfad_lock, flags);
  1038. }
  1039. list_del(&itnim->list_entry);
  1040. }
  1041. kfree(itnim);
  1042. break;
  1043. default:
  1044. WARN_ON(1);
  1045. break;
  1046. }
  1047. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  1048. }
  1049. /*
  1050. * Scsi_Host template entry, queue a SCSI command to the BFAD.
  1051. */
  1052. static int
  1053. bfad_im_queuecommand_lck(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
  1054. {
  1055. struct bfad_im_port_s *im_port =
  1056. (struct bfad_im_port_s *) cmnd->device->host->hostdata[0];
  1057. struct bfad_s *bfad = im_port->bfad;
  1058. struct bfad_itnim_data_s *itnim_data = cmnd->device->hostdata;
  1059. struct bfad_itnim_s *itnim;
  1060. struct bfa_ioim_s *hal_io;
  1061. unsigned long flags;
  1062. int rc;
  1063. int sg_cnt = 0;
  1064. struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
  1065. rc = fc_remote_port_chkready(rport);
  1066. if (rc) {
  1067. cmnd->result = rc;
  1068. done(cmnd);
  1069. return 0;
  1070. }
  1071. if (bfad->bfad_flags & BFAD_EEH_BUSY) {
  1072. if (bfad->bfad_flags & BFAD_EEH_PCI_CHANNEL_IO_PERM_FAILURE)
  1073. cmnd->result = DID_NO_CONNECT << 16;
  1074. else
  1075. cmnd->result = DID_REQUEUE << 16;
  1076. done(cmnd);
  1077. return 0;
  1078. }
  1079. sg_cnt = scsi_dma_map(cmnd);
  1080. if (sg_cnt < 0)
  1081. return SCSI_MLQUEUE_HOST_BUSY;
  1082. cmnd->scsi_done = done;
  1083. spin_lock_irqsave(&bfad->bfad_lock, flags);
  1084. if (!(bfad->bfad_flags & BFAD_HAL_START_DONE)) {
  1085. printk(KERN_WARNING
  1086. "bfad%d, queuecommand %p %x failed, BFA stopped\n",
  1087. bfad->inst_no, cmnd, cmnd->cmnd[0]);
  1088. cmnd->result = ScsiResult(DID_NO_CONNECT, 0);
  1089. goto out_fail_cmd;
  1090. }
  1091. itnim = itnim_data->itnim;
  1092. if (!itnim) {
  1093. cmnd->result = ScsiResult(DID_IMM_RETRY, 0);
  1094. goto out_fail_cmd;
  1095. }
  1096. hal_io = bfa_ioim_alloc(&bfad->bfa, (struct bfad_ioim_s *) cmnd,
  1097. itnim->bfa_itnim, sg_cnt);
  1098. if (!hal_io) {
  1099. printk(KERN_WARNING "hal_io failure\n");
  1100. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  1101. scsi_dma_unmap(cmnd);
  1102. return SCSI_MLQUEUE_HOST_BUSY;
  1103. }
  1104. cmnd->host_scribble = (char *)hal_io;
  1105. bfa_ioim_start(hal_io);
  1106. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  1107. return 0;
  1108. out_fail_cmd:
  1109. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  1110. scsi_dma_unmap(cmnd);
  1111. if (done)
  1112. done(cmnd);
  1113. return 0;
  1114. }
  1115. static DEF_SCSI_QCMD(bfad_im_queuecommand)
  1116. void
  1117. bfad_rport_online_wait(struct bfad_s *bfad)
  1118. {
  1119. int i;
  1120. int rport_delay = 10;
  1121. for (i = 0; !(bfad->bfad_flags & BFAD_PORT_ONLINE)
  1122. && i < bfa_linkup_delay; i++) {
  1123. set_current_state(TASK_UNINTERRUPTIBLE);
  1124. schedule_timeout(HZ);
  1125. }
  1126. if (bfad->bfad_flags & BFAD_PORT_ONLINE) {
  1127. rport_delay = rport_delay < bfa_linkup_delay ?
  1128. rport_delay : bfa_linkup_delay;
  1129. for (i = 0; !(bfad->bfad_flags & BFAD_RPORT_ONLINE)
  1130. && i < rport_delay; i++) {
  1131. set_current_state(TASK_UNINTERRUPTIBLE);
  1132. schedule_timeout(HZ);
  1133. }
  1134. if (rport_delay > 0 && (bfad->bfad_flags & BFAD_RPORT_ONLINE)) {
  1135. set_current_state(TASK_UNINTERRUPTIBLE);
  1136. schedule_timeout(rport_delay * HZ);
  1137. }
  1138. }
  1139. }
  1140. int
  1141. bfad_get_linkup_delay(struct bfad_s *bfad)
  1142. {
  1143. u8 nwwns = 0;
  1144. wwn_t wwns[BFA_PREBOOT_BOOTLUN_MAX];
  1145. int linkup_delay;
  1146. /*
  1147. * Querying for the boot target port wwns
  1148. * -- read from boot information in flash.
  1149. * If nwwns > 0 => boot over SAN and set linkup_delay = 30
  1150. * else => local boot machine set linkup_delay = 0
  1151. */
  1152. bfa_iocfc_get_bootwwns(&bfad->bfa, &nwwns, wwns);
  1153. if (nwwns > 0)
  1154. /* If Boot over SAN set linkup_delay = 30sec */
  1155. linkup_delay = 30;
  1156. else
  1157. /* If local boot; no linkup_delay */
  1158. linkup_delay = 0;
  1159. return linkup_delay;
  1160. }