bfad_im.c 34 KB

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