mptfc.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553
  1. /*
  2. * linux/drivers/message/fusion/mptfc.c
  3. * For use with LSI PCI chip/adapter(s)
  4. * running LSI Fusion MPT (Message Passing Technology) firmware.
  5. *
  6. * Copyright (c) 1999-2008 LSI Corporation
  7. * (mailto:DL-MPTFusionLinux@lsi.com)
  8. *
  9. */
  10. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  11. /*
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation; version 2 of the License.
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  19. NO WARRANTY
  20. THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
  21. CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
  22. LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
  23. MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
  24. solely responsible for determining the appropriateness of using and
  25. distributing the Program and assumes all risks associated with its
  26. exercise of rights under this Agreement, including but not limited to
  27. the risks and costs of program errors, damage to or loss of data,
  28. programs or equipment, and unavailability or interruption of operations.
  29. DISCLAIMER OF LIABILITY
  30. NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
  31. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32. DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
  33. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  34. TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  35. USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
  36. HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
  37. You should have received a copy of the GNU General Public License
  38. along with this program; if not, write to the Free Software
  39. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  40. */
  41. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  42. #include <linux/module.h>
  43. #include <linux/kernel.h>
  44. #include <linux/init.h>
  45. #include <linux/errno.h>
  46. #include <linux/kdev_t.h>
  47. #include <linux/blkdev.h>
  48. #include <linux/delay.h> /* for mdelay */
  49. #include <linux/interrupt.h> /* needed for in_interrupt() proto */
  50. #include <linux/reboot.h> /* notifier code */
  51. #include <linux/workqueue.h>
  52. #include <linux/sort.h>
  53. #include <linux/slab.h>
  54. #include <scsi/scsi.h>
  55. #include <scsi/scsi_cmnd.h>
  56. #include <scsi/scsi_device.h>
  57. #include <scsi/scsi_host.h>
  58. #include <scsi/scsi_tcq.h>
  59. #include <scsi/scsi_transport_fc.h>
  60. #include "mptbase.h"
  61. #include "mptscsih.h"
  62. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  63. #define my_NAME "Fusion MPT FC Host driver"
  64. #define my_VERSION MPT_LINUX_VERSION_COMMON
  65. #define MYNAM "mptfc"
  66. MODULE_AUTHOR(MODULEAUTHOR);
  67. MODULE_DESCRIPTION(my_NAME);
  68. MODULE_LICENSE("GPL");
  69. MODULE_VERSION(my_VERSION);
  70. /* Command line args */
  71. #define MPTFC_DEV_LOSS_TMO (60)
  72. static int mptfc_dev_loss_tmo = MPTFC_DEV_LOSS_TMO; /* reasonable default */
  73. module_param(mptfc_dev_loss_tmo, int, 0);
  74. MODULE_PARM_DESC(mptfc_dev_loss_tmo, " Initial time the driver programs the "
  75. " transport to wait for an rport to "
  76. " return following a device loss event."
  77. " Default=60.");
  78. /* scsi-mid layer global parmeter is max_report_luns, which is 511 */
  79. #define MPTFC_MAX_LUN (16895)
  80. static int max_lun = MPTFC_MAX_LUN;
  81. module_param(max_lun, int, 0);
  82. MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
  83. static u8 mptfcDoneCtx = MPT_MAX_PROTOCOL_DRIVERS;
  84. static u8 mptfcTaskCtx = MPT_MAX_PROTOCOL_DRIVERS;
  85. static u8 mptfcInternalCtx = MPT_MAX_PROTOCOL_DRIVERS;
  86. static int mptfc_target_alloc(struct scsi_target *starget);
  87. static int mptfc_slave_alloc(struct scsi_device *sdev);
  88. static int mptfc_qcmd(struct Scsi_Host *shost, struct scsi_cmnd *SCpnt);
  89. static void mptfc_target_destroy(struct scsi_target *starget);
  90. static void mptfc_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout);
  91. static void mptfc_remove(struct pci_dev *pdev);
  92. static int mptfc_abort(struct scsi_cmnd *SCpnt);
  93. static int mptfc_dev_reset(struct scsi_cmnd *SCpnt);
  94. static int mptfc_bus_reset(struct scsi_cmnd *SCpnt);
  95. static int mptfc_host_reset(struct scsi_cmnd *SCpnt);
  96. static struct scsi_host_template mptfc_driver_template = {
  97. .module = THIS_MODULE,
  98. .proc_name = "mptfc",
  99. .show_info = mptscsih_show_info,
  100. .name = "MPT FC Host",
  101. .info = mptscsih_info,
  102. .queuecommand = mptfc_qcmd,
  103. .target_alloc = mptfc_target_alloc,
  104. .slave_alloc = mptfc_slave_alloc,
  105. .slave_configure = mptscsih_slave_configure,
  106. .target_destroy = mptfc_target_destroy,
  107. .slave_destroy = mptscsih_slave_destroy,
  108. .change_queue_depth = mptscsih_change_queue_depth,
  109. .eh_abort_handler = mptfc_abort,
  110. .eh_device_reset_handler = mptfc_dev_reset,
  111. .eh_bus_reset_handler = mptfc_bus_reset,
  112. .eh_host_reset_handler = mptfc_host_reset,
  113. .bios_param = mptscsih_bios_param,
  114. .can_queue = MPT_FC_CAN_QUEUE,
  115. .this_id = -1,
  116. .sg_tablesize = MPT_SCSI_SG_DEPTH,
  117. .max_sectors = 8192,
  118. .cmd_per_lun = 7,
  119. .use_clustering = ENABLE_CLUSTERING,
  120. .shost_attrs = mptscsih_host_attrs,
  121. };
  122. /****************************************************************************
  123. * Supported hardware
  124. */
  125. static struct pci_device_id mptfc_pci_table[] = {
  126. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC909,
  127. PCI_ANY_ID, PCI_ANY_ID },
  128. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC919,
  129. PCI_ANY_ID, PCI_ANY_ID },
  130. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC929,
  131. PCI_ANY_ID, PCI_ANY_ID },
  132. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC919X,
  133. PCI_ANY_ID, PCI_ANY_ID },
  134. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC929X,
  135. PCI_ANY_ID, PCI_ANY_ID },
  136. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC939X,
  137. PCI_ANY_ID, PCI_ANY_ID },
  138. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC949X,
  139. PCI_ANY_ID, PCI_ANY_ID },
  140. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC949E,
  141. PCI_ANY_ID, PCI_ANY_ID },
  142. { PCI_VENDOR_ID_BROCADE, MPI_MANUFACTPAGE_DEVICEID_FC949E,
  143. PCI_ANY_ID, PCI_ANY_ID },
  144. {0} /* Terminating entry */
  145. };
  146. MODULE_DEVICE_TABLE(pci, mptfc_pci_table);
  147. static struct scsi_transport_template *mptfc_transport_template = NULL;
  148. static struct fc_function_template mptfc_transport_functions = {
  149. .dd_fcrport_size = 8,
  150. .show_host_node_name = 1,
  151. .show_host_port_name = 1,
  152. .show_host_supported_classes = 1,
  153. .show_host_port_id = 1,
  154. .show_rport_supported_classes = 1,
  155. .show_starget_node_name = 1,
  156. .show_starget_port_name = 1,
  157. .show_starget_port_id = 1,
  158. .set_rport_dev_loss_tmo = mptfc_set_rport_loss_tmo,
  159. .show_rport_dev_loss_tmo = 1,
  160. .show_host_supported_speeds = 1,
  161. .show_host_maxframe_size = 1,
  162. .show_host_speed = 1,
  163. .show_host_fabric_name = 1,
  164. .show_host_port_type = 1,
  165. .show_host_port_state = 1,
  166. .show_host_symbolic_name = 1,
  167. };
  168. static int
  169. mptfc_block_error_handler(struct scsi_cmnd *SCpnt,
  170. int (*func)(struct scsi_cmnd *SCpnt),
  171. const char *caller)
  172. {
  173. MPT_SCSI_HOST *hd;
  174. struct scsi_device *sdev = SCpnt->device;
  175. struct Scsi_Host *shost = sdev->host;
  176. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  177. unsigned long flags;
  178. int ready;
  179. MPT_ADAPTER *ioc;
  180. int loops = 40; /* seconds */
  181. hd = shost_priv(SCpnt->device->host);
  182. ioc = hd->ioc;
  183. spin_lock_irqsave(shost->host_lock, flags);
  184. while ((ready = fc_remote_port_chkready(rport) >> 16) == DID_IMM_RETRY
  185. || (loops > 0 && ioc->active == 0)) {
  186. spin_unlock_irqrestore(shost->host_lock, flags);
  187. dfcprintk (ioc, printk(MYIOC_s_DEBUG_FMT
  188. "mptfc_block_error_handler.%d: %d:%llu, port status is "
  189. "%x, active flag %d, deferring %s recovery.\n",
  190. ioc->name, ioc->sh->host_no,
  191. SCpnt->device->id, SCpnt->device->lun,
  192. ready, ioc->active, caller));
  193. msleep(1000);
  194. spin_lock_irqsave(shost->host_lock, flags);
  195. loops --;
  196. }
  197. spin_unlock_irqrestore(shost->host_lock, flags);
  198. if (ready == DID_NO_CONNECT || !SCpnt->device->hostdata
  199. || ioc->active == 0) {
  200. dfcprintk (ioc, printk(MYIOC_s_DEBUG_FMT
  201. "%s.%d: %d:%llu, failing recovery, "
  202. "port state %x, active %d, vdevice %p.\n", caller,
  203. ioc->name, ioc->sh->host_no,
  204. SCpnt->device->id, SCpnt->device->lun, ready,
  205. ioc->active, SCpnt->device->hostdata));
  206. return FAILED;
  207. }
  208. dfcprintk (ioc, printk(MYIOC_s_DEBUG_FMT
  209. "%s.%d: %d:%llu, executing recovery.\n", caller,
  210. ioc->name, ioc->sh->host_no,
  211. SCpnt->device->id, SCpnt->device->lun));
  212. return (*func)(SCpnt);
  213. }
  214. static int
  215. mptfc_abort(struct scsi_cmnd *SCpnt)
  216. {
  217. return
  218. mptfc_block_error_handler(SCpnt, mptscsih_abort, __func__);
  219. }
  220. static int
  221. mptfc_dev_reset(struct scsi_cmnd *SCpnt)
  222. {
  223. return
  224. mptfc_block_error_handler(SCpnt, mptscsih_dev_reset, __func__);
  225. }
  226. static int
  227. mptfc_bus_reset(struct scsi_cmnd *SCpnt)
  228. {
  229. return
  230. mptfc_block_error_handler(SCpnt, mptscsih_bus_reset, __func__);
  231. }
  232. static int
  233. mptfc_host_reset(struct scsi_cmnd *SCpnt)
  234. {
  235. return
  236. mptfc_block_error_handler(SCpnt, mptscsih_host_reset, __func__);
  237. }
  238. static void
  239. mptfc_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout)
  240. {
  241. if (timeout > 0)
  242. rport->dev_loss_tmo = timeout;
  243. else
  244. rport->dev_loss_tmo = mptfc_dev_loss_tmo;
  245. }
  246. static int
  247. mptfc_FcDevPage0_cmp_func(const void *a, const void *b)
  248. {
  249. FCDevicePage0_t **aa = (FCDevicePage0_t **)a;
  250. FCDevicePage0_t **bb = (FCDevicePage0_t **)b;
  251. if ((*aa)->CurrentBus == (*bb)->CurrentBus) {
  252. if ((*aa)->CurrentTargetID == (*bb)->CurrentTargetID)
  253. return 0;
  254. if ((*aa)->CurrentTargetID < (*bb)->CurrentTargetID)
  255. return -1;
  256. return 1;
  257. }
  258. if ((*aa)->CurrentBus < (*bb)->CurrentBus)
  259. return -1;
  260. return 1;
  261. }
  262. static int
  263. mptfc_GetFcDevPage0(MPT_ADAPTER *ioc, int ioc_port,
  264. void(*func)(MPT_ADAPTER *ioc,int channel, FCDevicePage0_t *arg))
  265. {
  266. ConfigPageHeader_t hdr;
  267. CONFIGPARMS cfg;
  268. FCDevicePage0_t *ppage0_alloc, *fc;
  269. dma_addr_t page0_dma;
  270. int data_sz;
  271. int ii;
  272. FCDevicePage0_t *p0_array=NULL, *p_p0;
  273. FCDevicePage0_t **pp0_array=NULL, **p_pp0;
  274. int rc = -ENOMEM;
  275. U32 port_id = 0xffffff;
  276. int num_targ = 0;
  277. int max_bus = ioc->facts.MaxBuses;
  278. int max_targ;
  279. max_targ = (ioc->facts.MaxDevices == 0) ? 256 : ioc->facts.MaxDevices;
  280. data_sz = sizeof(FCDevicePage0_t) * max_bus * max_targ;
  281. p_p0 = p0_array = kzalloc(data_sz, GFP_KERNEL);
  282. if (!p0_array)
  283. goto out;
  284. data_sz = sizeof(FCDevicePage0_t *) * max_bus * max_targ;
  285. p_pp0 = pp0_array = kzalloc(data_sz, GFP_KERNEL);
  286. if (!pp0_array)
  287. goto out;
  288. do {
  289. /* Get FC Device Page 0 header */
  290. hdr.PageVersion = 0;
  291. hdr.PageLength = 0;
  292. hdr.PageNumber = 0;
  293. hdr.PageType = MPI_CONFIG_PAGETYPE_FC_DEVICE;
  294. cfg.cfghdr.hdr = &hdr;
  295. cfg.physAddr = -1;
  296. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  297. cfg.dir = 0;
  298. cfg.pageAddr = port_id;
  299. cfg.timeout = 0;
  300. if ((rc = mpt_config(ioc, &cfg)) != 0)
  301. break;
  302. if (hdr.PageLength <= 0)
  303. break;
  304. data_sz = hdr.PageLength * 4;
  305. ppage0_alloc = pci_alloc_consistent(ioc->pcidev, data_sz,
  306. &page0_dma);
  307. rc = -ENOMEM;
  308. if (!ppage0_alloc)
  309. break;
  310. cfg.physAddr = page0_dma;
  311. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  312. if ((rc = mpt_config(ioc, &cfg)) == 0) {
  313. ppage0_alloc->PortIdentifier =
  314. le32_to_cpu(ppage0_alloc->PortIdentifier);
  315. ppage0_alloc->WWNN.Low =
  316. le32_to_cpu(ppage0_alloc->WWNN.Low);
  317. ppage0_alloc->WWNN.High =
  318. le32_to_cpu(ppage0_alloc->WWNN.High);
  319. ppage0_alloc->WWPN.Low =
  320. le32_to_cpu(ppage0_alloc->WWPN.Low);
  321. ppage0_alloc->WWPN.High =
  322. le32_to_cpu(ppage0_alloc->WWPN.High);
  323. ppage0_alloc->BBCredit =
  324. le16_to_cpu(ppage0_alloc->BBCredit);
  325. ppage0_alloc->MaxRxFrameSize =
  326. le16_to_cpu(ppage0_alloc->MaxRxFrameSize);
  327. port_id = ppage0_alloc->PortIdentifier;
  328. num_targ++;
  329. *p_p0 = *ppage0_alloc; /* save data */
  330. *p_pp0++ = p_p0++; /* save addr */
  331. }
  332. pci_free_consistent(ioc->pcidev, data_sz,
  333. (u8 *) ppage0_alloc, page0_dma);
  334. if (rc != 0)
  335. break;
  336. } while (port_id <= 0xff0000);
  337. if (num_targ) {
  338. /* sort array */
  339. if (num_targ > 1)
  340. sort (pp0_array, num_targ, sizeof(FCDevicePage0_t *),
  341. mptfc_FcDevPage0_cmp_func, NULL);
  342. /* call caller's func for each targ */
  343. for (ii = 0; ii < num_targ; ii++) {
  344. fc = *(pp0_array+ii);
  345. func(ioc, ioc_port, fc);
  346. }
  347. }
  348. out:
  349. kfree(pp0_array);
  350. kfree(p0_array);
  351. return rc;
  352. }
  353. static int
  354. mptfc_generate_rport_ids(FCDevicePage0_t *pg0, struct fc_rport_identifiers *rid)
  355. {
  356. /* not currently usable */
  357. if (pg0->Flags & (MPI_FC_DEVICE_PAGE0_FLAGS_PLOGI_INVALID |
  358. MPI_FC_DEVICE_PAGE0_FLAGS_PRLI_INVALID))
  359. return -1;
  360. if (!(pg0->Flags & MPI_FC_DEVICE_PAGE0_FLAGS_TARGETID_BUS_VALID))
  361. return -1;
  362. if (!(pg0->Protocol & MPI_FC_DEVICE_PAGE0_PROT_FCP_TARGET))
  363. return -1;
  364. /*
  365. * board data structure already normalized to platform endianness
  366. * shifted to avoid unaligned access on 64 bit architecture
  367. */
  368. rid->node_name = ((u64)pg0->WWNN.High) << 32 | (u64)pg0->WWNN.Low;
  369. rid->port_name = ((u64)pg0->WWPN.High) << 32 | (u64)pg0->WWPN.Low;
  370. rid->port_id = pg0->PortIdentifier;
  371. rid->roles = FC_RPORT_ROLE_UNKNOWN;
  372. return 0;
  373. }
  374. static void
  375. mptfc_register_dev(MPT_ADAPTER *ioc, int channel, FCDevicePage0_t *pg0)
  376. {
  377. struct fc_rport_identifiers rport_ids;
  378. struct fc_rport *rport;
  379. struct mptfc_rport_info *ri;
  380. int new_ri = 1;
  381. u64 pn, nn;
  382. VirtTarget *vtarget;
  383. u32 roles = FC_RPORT_ROLE_UNKNOWN;
  384. if (mptfc_generate_rport_ids(pg0, &rport_ids) < 0)
  385. return;
  386. roles |= FC_RPORT_ROLE_FCP_TARGET;
  387. if (pg0->Protocol & MPI_FC_DEVICE_PAGE0_PROT_FCP_INITIATOR)
  388. roles |= FC_RPORT_ROLE_FCP_INITIATOR;
  389. /* scan list looking for a match */
  390. list_for_each_entry(ri, &ioc->fc_rports, list) {
  391. pn = (u64)ri->pg0.WWPN.High << 32 | (u64)ri->pg0.WWPN.Low;
  392. if (pn == rport_ids.port_name) { /* match */
  393. list_move_tail(&ri->list, &ioc->fc_rports);
  394. new_ri = 0;
  395. break;
  396. }
  397. }
  398. if (new_ri) { /* allocate one */
  399. ri = kzalloc(sizeof(struct mptfc_rport_info), GFP_KERNEL);
  400. if (!ri)
  401. return;
  402. list_add_tail(&ri->list, &ioc->fc_rports);
  403. }
  404. ri->pg0 = *pg0; /* add/update pg0 data */
  405. ri->flags &= ~MPT_RPORT_INFO_FLAGS_MISSING;
  406. /* MPT_RPORT_INFO_FLAGS_REGISTERED - rport not previously deleted */
  407. if (!(ri->flags & MPT_RPORT_INFO_FLAGS_REGISTERED)) {
  408. ri->flags |= MPT_RPORT_INFO_FLAGS_REGISTERED;
  409. rport = fc_remote_port_add(ioc->sh, channel, &rport_ids);
  410. if (rport) {
  411. ri->rport = rport;
  412. if (new_ri) /* may have been reset by user */
  413. rport->dev_loss_tmo = mptfc_dev_loss_tmo;
  414. /*
  415. * if already mapped, remap here. If not mapped,
  416. * target_alloc will allocate vtarget and map,
  417. * slave_alloc will fill in vdevice from vtarget.
  418. */
  419. if (ri->starget) {
  420. vtarget = ri->starget->hostdata;
  421. if (vtarget) {
  422. vtarget->id = pg0->CurrentTargetID;
  423. vtarget->channel = pg0->CurrentBus;
  424. vtarget->deleted = 0;
  425. }
  426. }
  427. *((struct mptfc_rport_info **)rport->dd_data) = ri;
  428. /* scan will be scheduled once rport becomes a target */
  429. fc_remote_port_rolechg(rport,roles);
  430. pn = (u64)ri->pg0.WWPN.High << 32 | (u64)ri->pg0.WWPN.Low;
  431. nn = (u64)ri->pg0.WWNN.High << 32 | (u64)ri->pg0.WWNN.Low;
  432. dfcprintk (ioc, printk(MYIOC_s_DEBUG_FMT
  433. "mptfc_reg_dev.%d: %x, %llx / %llx, tid %d, "
  434. "rport tid %d, tmo %d\n",
  435. ioc->name,
  436. ioc->sh->host_no,
  437. pg0->PortIdentifier,
  438. (unsigned long long)nn,
  439. (unsigned long long)pn,
  440. pg0->CurrentTargetID,
  441. ri->rport->scsi_target_id,
  442. ri->rport->dev_loss_tmo));
  443. } else {
  444. list_del(&ri->list);
  445. kfree(ri);
  446. ri = NULL;
  447. }
  448. }
  449. }
  450. /*
  451. * OS entry point to allow for host driver to free allocated memory
  452. * Called if no device present or device being unloaded
  453. */
  454. static void
  455. mptfc_target_destroy(struct scsi_target *starget)
  456. {
  457. struct fc_rport *rport;
  458. struct mptfc_rport_info *ri;
  459. rport = starget_to_rport(starget);
  460. if (rport) {
  461. ri = *((struct mptfc_rport_info **)rport->dd_data);
  462. if (ri) /* better be! */
  463. ri->starget = NULL;
  464. }
  465. kfree(starget->hostdata);
  466. starget->hostdata = NULL;
  467. }
  468. /*
  469. * OS entry point to allow host driver to alloc memory
  470. * for each scsi target. Called once per device the bus scan.
  471. * Return non-zero if allocation fails.
  472. */
  473. static int
  474. mptfc_target_alloc(struct scsi_target *starget)
  475. {
  476. VirtTarget *vtarget;
  477. struct fc_rport *rport;
  478. struct mptfc_rport_info *ri;
  479. int rc;
  480. vtarget = kzalloc(sizeof(VirtTarget), GFP_KERNEL);
  481. if (!vtarget)
  482. return -ENOMEM;
  483. starget->hostdata = vtarget;
  484. rc = -ENODEV;
  485. rport = starget_to_rport(starget);
  486. if (rport) {
  487. ri = *((struct mptfc_rport_info **)rport->dd_data);
  488. if (ri) { /* better be! */
  489. vtarget->id = ri->pg0.CurrentTargetID;
  490. vtarget->channel = ri->pg0.CurrentBus;
  491. ri->starget = starget;
  492. rc = 0;
  493. }
  494. }
  495. if (rc != 0) {
  496. kfree(vtarget);
  497. starget->hostdata = NULL;
  498. }
  499. return rc;
  500. }
  501. /*
  502. * mptfc_dump_lun_info
  503. * @ioc
  504. * @rport
  505. * @sdev
  506. *
  507. */
  508. static void
  509. mptfc_dump_lun_info(MPT_ADAPTER *ioc, struct fc_rport *rport, struct scsi_device *sdev,
  510. VirtTarget *vtarget)
  511. {
  512. u64 nn, pn;
  513. struct mptfc_rport_info *ri;
  514. ri = *((struct mptfc_rport_info **)rport->dd_data);
  515. pn = (u64)ri->pg0.WWPN.High << 32 | (u64)ri->pg0.WWPN.Low;
  516. nn = (u64)ri->pg0.WWNN.High << 32 | (u64)ri->pg0.WWNN.Low;
  517. dfcprintk (ioc, printk(MYIOC_s_DEBUG_FMT
  518. "mptfc_slv_alloc.%d: num_luns %d, sdev.id %d, "
  519. "CurrentTargetID %d, %x %llx %llx\n",
  520. ioc->name,
  521. sdev->host->host_no,
  522. vtarget->num_luns,
  523. sdev->id, ri->pg0.CurrentTargetID,
  524. ri->pg0.PortIdentifier,
  525. (unsigned long long)pn,
  526. (unsigned long long)nn));
  527. }
  528. /*
  529. * OS entry point to allow host driver to alloc memory
  530. * for each scsi device. Called once per device the bus scan.
  531. * Return non-zero if allocation fails.
  532. * Init memory once per LUN.
  533. */
  534. static int
  535. mptfc_slave_alloc(struct scsi_device *sdev)
  536. {
  537. MPT_SCSI_HOST *hd;
  538. VirtTarget *vtarget;
  539. VirtDevice *vdevice;
  540. struct scsi_target *starget;
  541. struct fc_rport *rport;
  542. MPT_ADAPTER *ioc;
  543. starget = scsi_target(sdev);
  544. rport = starget_to_rport(starget);
  545. if (!rport || fc_remote_port_chkready(rport))
  546. return -ENXIO;
  547. hd = shost_priv(sdev->host);
  548. ioc = hd->ioc;
  549. vdevice = kzalloc(sizeof(VirtDevice), GFP_KERNEL);
  550. if (!vdevice) {
  551. printk(MYIOC_s_ERR_FMT "slave_alloc kmalloc(%zd) FAILED!\n",
  552. ioc->name, sizeof(VirtDevice));
  553. return -ENOMEM;
  554. }
  555. sdev->hostdata = vdevice;
  556. vtarget = starget->hostdata;
  557. if (vtarget->num_luns == 0) {
  558. vtarget->ioc_id = ioc->id;
  559. vtarget->tflags = MPT_TARGET_FLAGS_Q_YES;
  560. }
  561. vdevice->vtarget = vtarget;
  562. vdevice->lun = sdev->lun;
  563. vtarget->num_luns++;
  564. mptfc_dump_lun_info(ioc, rport, sdev, vtarget);
  565. return 0;
  566. }
  567. static int
  568. mptfc_qcmd(struct Scsi_Host *shost, struct scsi_cmnd *SCpnt)
  569. {
  570. struct mptfc_rport_info *ri;
  571. struct fc_rport *rport = starget_to_rport(scsi_target(SCpnt->device));
  572. int err;
  573. VirtDevice *vdevice = SCpnt->device->hostdata;
  574. if (!vdevice || !vdevice->vtarget) {
  575. SCpnt->result = DID_NO_CONNECT << 16;
  576. SCpnt->scsi_done(SCpnt);
  577. return 0;
  578. }
  579. err = fc_remote_port_chkready(rport);
  580. if (unlikely(err)) {
  581. SCpnt->result = err;
  582. SCpnt->scsi_done(SCpnt);
  583. return 0;
  584. }
  585. /* dd_data is null until finished adding target */
  586. ri = *((struct mptfc_rport_info **)rport->dd_data);
  587. if (unlikely(!ri)) {
  588. SCpnt->result = DID_IMM_RETRY << 16;
  589. SCpnt->scsi_done(SCpnt);
  590. return 0;
  591. }
  592. return mptscsih_qcmd(SCpnt);
  593. }
  594. /*
  595. * mptfc_display_port_link_speed - displaying link speed
  596. * @ioc: Pointer to MPT_ADAPTER structure
  597. * @portnum: IOC Port number
  598. * @pp0dest: port page0 data payload
  599. *
  600. */
  601. static void
  602. mptfc_display_port_link_speed(MPT_ADAPTER *ioc, int portnum, FCPortPage0_t *pp0dest)
  603. {
  604. u8 old_speed, new_speed, state;
  605. char *old, *new;
  606. if (portnum >= 2)
  607. return;
  608. old_speed = ioc->fc_link_speed[portnum];
  609. new_speed = pp0dest->CurrentSpeed;
  610. state = pp0dest->PortState;
  611. if (state != MPI_FCPORTPAGE0_PORTSTATE_OFFLINE &&
  612. new_speed != MPI_FCPORTPAGE0_CURRENT_SPEED_UKNOWN) {
  613. old = old_speed == MPI_FCPORTPAGE0_CURRENT_SPEED_1GBIT ? "1 Gbps" :
  614. old_speed == MPI_FCPORTPAGE0_CURRENT_SPEED_2GBIT ? "2 Gbps" :
  615. old_speed == MPI_FCPORTPAGE0_CURRENT_SPEED_4GBIT ? "4 Gbps" :
  616. "Unknown";
  617. new = new_speed == MPI_FCPORTPAGE0_CURRENT_SPEED_1GBIT ? "1 Gbps" :
  618. new_speed == MPI_FCPORTPAGE0_CURRENT_SPEED_2GBIT ? "2 Gbps" :
  619. new_speed == MPI_FCPORTPAGE0_CURRENT_SPEED_4GBIT ? "4 Gbps" :
  620. "Unknown";
  621. if (old_speed == 0)
  622. printk(MYIOC_s_NOTE_FMT
  623. "FC Link Established, Speed = %s\n",
  624. ioc->name, new);
  625. else if (old_speed != new_speed)
  626. printk(MYIOC_s_WARN_FMT
  627. "FC Link Speed Change, Old Speed = %s, New Speed = %s\n",
  628. ioc->name, old, new);
  629. ioc->fc_link_speed[portnum] = new_speed;
  630. }
  631. }
  632. /*
  633. * mptfc_GetFcPortPage0 - Fetch FCPort config Page0.
  634. * @ioc: Pointer to MPT_ADAPTER structure
  635. * @portnum: IOC Port number
  636. *
  637. * Return: 0 for success
  638. * -ENOMEM if no memory available
  639. * -EPERM if not allowed due to ISR context
  640. * -EAGAIN if no msg frames currently available
  641. * -EFAULT for non-successful reply or no reply (timeout)
  642. * -EINVAL portnum arg out of range (hardwired to two elements)
  643. */
  644. static int
  645. mptfc_GetFcPortPage0(MPT_ADAPTER *ioc, int portnum)
  646. {
  647. ConfigPageHeader_t hdr;
  648. CONFIGPARMS cfg;
  649. FCPortPage0_t *ppage0_alloc;
  650. FCPortPage0_t *pp0dest;
  651. dma_addr_t page0_dma;
  652. int data_sz;
  653. int copy_sz;
  654. int rc;
  655. int count = 400;
  656. if (portnum > 1)
  657. return -EINVAL;
  658. /* Get FCPort Page 0 header */
  659. hdr.PageVersion = 0;
  660. hdr.PageLength = 0;
  661. hdr.PageNumber = 0;
  662. hdr.PageType = MPI_CONFIG_PAGETYPE_FC_PORT;
  663. cfg.cfghdr.hdr = &hdr;
  664. cfg.physAddr = -1;
  665. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  666. cfg.dir = 0;
  667. cfg.pageAddr = portnum;
  668. cfg.timeout = 0;
  669. if ((rc = mpt_config(ioc, &cfg)) != 0)
  670. return rc;
  671. if (hdr.PageLength == 0)
  672. return 0;
  673. data_sz = hdr.PageLength * 4;
  674. rc = -ENOMEM;
  675. ppage0_alloc = (FCPortPage0_t *) pci_alloc_consistent(ioc->pcidev, data_sz, &page0_dma);
  676. if (ppage0_alloc) {
  677. try_again:
  678. memset((u8 *)ppage0_alloc, 0, data_sz);
  679. cfg.physAddr = page0_dma;
  680. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  681. if ((rc = mpt_config(ioc, &cfg)) == 0) {
  682. /* save the data */
  683. pp0dest = &ioc->fc_port_page0[portnum];
  684. copy_sz = min_t(int, sizeof(FCPortPage0_t), data_sz);
  685. memcpy(pp0dest, ppage0_alloc, copy_sz);
  686. /*
  687. * Normalize endianness of structure data,
  688. * by byte-swapping all > 1 byte fields!
  689. */
  690. pp0dest->Flags = le32_to_cpu(pp0dest->Flags);
  691. pp0dest->PortIdentifier = le32_to_cpu(pp0dest->PortIdentifier);
  692. pp0dest->WWNN.Low = le32_to_cpu(pp0dest->WWNN.Low);
  693. pp0dest->WWNN.High = le32_to_cpu(pp0dest->WWNN.High);
  694. pp0dest->WWPN.Low = le32_to_cpu(pp0dest->WWPN.Low);
  695. pp0dest->WWPN.High = le32_to_cpu(pp0dest->WWPN.High);
  696. pp0dest->SupportedServiceClass = le32_to_cpu(pp0dest->SupportedServiceClass);
  697. pp0dest->SupportedSpeeds = le32_to_cpu(pp0dest->SupportedSpeeds);
  698. pp0dest->CurrentSpeed = le32_to_cpu(pp0dest->CurrentSpeed);
  699. pp0dest->MaxFrameSize = le32_to_cpu(pp0dest->MaxFrameSize);
  700. pp0dest->FabricWWNN.Low = le32_to_cpu(pp0dest->FabricWWNN.Low);
  701. pp0dest->FabricWWNN.High = le32_to_cpu(pp0dest->FabricWWNN.High);
  702. pp0dest->FabricWWPN.Low = le32_to_cpu(pp0dest->FabricWWPN.Low);
  703. pp0dest->FabricWWPN.High = le32_to_cpu(pp0dest->FabricWWPN.High);
  704. pp0dest->DiscoveredPortsCount = le32_to_cpu(pp0dest->DiscoveredPortsCount);
  705. pp0dest->MaxInitiators = le32_to_cpu(pp0dest->MaxInitiators);
  706. /*
  707. * if still doing discovery,
  708. * hang loose a while until finished
  709. */
  710. if ((pp0dest->PortState == MPI_FCPORTPAGE0_PORTSTATE_UNKNOWN) ||
  711. (pp0dest->PortState == MPI_FCPORTPAGE0_PORTSTATE_ONLINE &&
  712. (pp0dest->Flags & MPI_FCPORTPAGE0_FLAGS_ATTACH_TYPE_MASK)
  713. == MPI_FCPORTPAGE0_FLAGS_ATTACH_NO_INIT)) {
  714. if (count-- > 0) {
  715. msleep(100);
  716. goto try_again;
  717. }
  718. printk(MYIOC_s_INFO_FMT "Firmware discovery not"
  719. " complete.\n",
  720. ioc->name);
  721. }
  722. mptfc_display_port_link_speed(ioc, portnum, pp0dest);
  723. }
  724. pci_free_consistent(ioc->pcidev, data_sz, (u8 *) ppage0_alloc, page0_dma);
  725. }
  726. return rc;
  727. }
  728. static int
  729. mptfc_WriteFcPortPage1(MPT_ADAPTER *ioc, int portnum)
  730. {
  731. ConfigPageHeader_t hdr;
  732. CONFIGPARMS cfg;
  733. int rc;
  734. if (portnum > 1)
  735. return -EINVAL;
  736. if (!(ioc->fc_data.fc_port_page1[portnum].data))
  737. return -EINVAL;
  738. /* get fcport page 1 header */
  739. hdr.PageVersion = 0;
  740. hdr.PageLength = 0;
  741. hdr.PageNumber = 1;
  742. hdr.PageType = MPI_CONFIG_PAGETYPE_FC_PORT;
  743. cfg.cfghdr.hdr = &hdr;
  744. cfg.physAddr = -1;
  745. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  746. cfg.dir = 0;
  747. cfg.pageAddr = portnum;
  748. cfg.timeout = 0;
  749. if ((rc = mpt_config(ioc, &cfg)) != 0)
  750. return rc;
  751. if (hdr.PageLength == 0)
  752. return -ENODEV;
  753. if (hdr.PageLength*4 != ioc->fc_data.fc_port_page1[portnum].pg_sz)
  754. return -EINVAL;
  755. cfg.physAddr = ioc->fc_data.fc_port_page1[portnum].dma;
  756. cfg.action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
  757. cfg.dir = 1;
  758. rc = mpt_config(ioc, &cfg);
  759. return rc;
  760. }
  761. static int
  762. mptfc_GetFcPortPage1(MPT_ADAPTER *ioc, int portnum)
  763. {
  764. ConfigPageHeader_t hdr;
  765. CONFIGPARMS cfg;
  766. FCPortPage1_t *page1_alloc;
  767. dma_addr_t page1_dma;
  768. int data_sz;
  769. int rc;
  770. if (portnum > 1)
  771. return -EINVAL;
  772. /* get fcport page 1 header */
  773. hdr.PageVersion = 0;
  774. hdr.PageLength = 0;
  775. hdr.PageNumber = 1;
  776. hdr.PageType = MPI_CONFIG_PAGETYPE_FC_PORT;
  777. cfg.cfghdr.hdr = &hdr;
  778. cfg.physAddr = -1;
  779. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  780. cfg.dir = 0;
  781. cfg.pageAddr = portnum;
  782. cfg.timeout = 0;
  783. if ((rc = mpt_config(ioc, &cfg)) != 0)
  784. return rc;
  785. if (hdr.PageLength == 0)
  786. return -ENODEV;
  787. start_over:
  788. if (ioc->fc_data.fc_port_page1[portnum].data == NULL) {
  789. data_sz = hdr.PageLength * 4;
  790. if (data_sz < sizeof(FCPortPage1_t))
  791. data_sz = sizeof(FCPortPage1_t);
  792. page1_alloc = (FCPortPage1_t *) pci_alloc_consistent(ioc->pcidev,
  793. data_sz,
  794. &page1_dma);
  795. if (!page1_alloc)
  796. return -ENOMEM;
  797. }
  798. else {
  799. page1_alloc = ioc->fc_data.fc_port_page1[portnum].data;
  800. page1_dma = ioc->fc_data.fc_port_page1[portnum].dma;
  801. data_sz = ioc->fc_data.fc_port_page1[portnum].pg_sz;
  802. if (hdr.PageLength * 4 > data_sz) {
  803. ioc->fc_data.fc_port_page1[portnum].data = NULL;
  804. pci_free_consistent(ioc->pcidev, data_sz, (u8 *)
  805. page1_alloc, page1_dma);
  806. goto start_over;
  807. }
  808. }
  809. memset(page1_alloc,0,data_sz);
  810. cfg.physAddr = page1_dma;
  811. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  812. if ((rc = mpt_config(ioc, &cfg)) == 0) {
  813. ioc->fc_data.fc_port_page1[portnum].data = page1_alloc;
  814. ioc->fc_data.fc_port_page1[portnum].pg_sz = data_sz;
  815. ioc->fc_data.fc_port_page1[portnum].dma = page1_dma;
  816. }
  817. else {
  818. ioc->fc_data.fc_port_page1[portnum].data = NULL;
  819. pci_free_consistent(ioc->pcidev, data_sz, (u8 *)
  820. page1_alloc, page1_dma);
  821. }
  822. return rc;
  823. }
  824. static void
  825. mptfc_SetFcPortPage1_defaults(MPT_ADAPTER *ioc)
  826. {
  827. int ii;
  828. FCPortPage1_t *pp1;
  829. #define MPTFC_FW_DEVICE_TIMEOUT (1)
  830. #define MPTFC_FW_IO_PEND_TIMEOUT (1)
  831. #define ON_FLAGS (MPI_FCPORTPAGE1_FLAGS_IMMEDIATE_ERROR_REPLY)
  832. #define OFF_FLAGS (MPI_FCPORTPAGE1_FLAGS_VERBOSE_RESCAN_EVENTS)
  833. for (ii=0; ii<ioc->facts.NumberOfPorts; ii++) {
  834. if (mptfc_GetFcPortPage1(ioc, ii) != 0)
  835. continue;
  836. pp1 = ioc->fc_data.fc_port_page1[ii].data;
  837. if ((pp1->InitiatorDeviceTimeout == MPTFC_FW_DEVICE_TIMEOUT)
  838. && (pp1->InitiatorIoPendTimeout == MPTFC_FW_IO_PEND_TIMEOUT)
  839. && ((pp1->Flags & ON_FLAGS) == ON_FLAGS)
  840. && ((pp1->Flags & OFF_FLAGS) == 0))
  841. continue;
  842. pp1->InitiatorDeviceTimeout = MPTFC_FW_DEVICE_TIMEOUT;
  843. pp1->InitiatorIoPendTimeout = MPTFC_FW_IO_PEND_TIMEOUT;
  844. pp1->Flags &= ~OFF_FLAGS;
  845. pp1->Flags |= ON_FLAGS;
  846. mptfc_WriteFcPortPage1(ioc, ii);
  847. }
  848. }
  849. static void
  850. mptfc_init_host_attr(MPT_ADAPTER *ioc,int portnum)
  851. {
  852. unsigned class = 0;
  853. unsigned cos = 0;
  854. unsigned speed;
  855. unsigned port_type;
  856. unsigned port_state;
  857. FCPortPage0_t *pp0;
  858. struct Scsi_Host *sh;
  859. char *sn;
  860. /* don't know what to do as only one scsi (fc) host was allocated */
  861. if (portnum != 0)
  862. return;
  863. pp0 = &ioc->fc_port_page0[portnum];
  864. sh = ioc->sh;
  865. sn = fc_host_symbolic_name(sh);
  866. snprintf(sn, FC_SYMBOLIC_NAME_SIZE, "%s %s%08xh",
  867. ioc->prod_name,
  868. MPT_FW_REV_MAGIC_ID_STRING,
  869. ioc->facts.FWVersion.Word);
  870. fc_host_tgtid_bind_type(sh) = FC_TGTID_BIND_BY_WWPN;
  871. fc_host_maxframe_size(sh) = pp0->MaxFrameSize;
  872. fc_host_node_name(sh) =
  873. (u64)pp0->WWNN.High << 32 | (u64)pp0->WWNN.Low;
  874. fc_host_port_name(sh) =
  875. (u64)pp0->WWPN.High << 32 | (u64)pp0->WWPN.Low;
  876. fc_host_port_id(sh) = pp0->PortIdentifier;
  877. class = pp0->SupportedServiceClass;
  878. if (class & MPI_FCPORTPAGE0_SUPPORT_CLASS_1)
  879. cos |= FC_COS_CLASS1;
  880. if (class & MPI_FCPORTPAGE0_SUPPORT_CLASS_2)
  881. cos |= FC_COS_CLASS2;
  882. if (class & MPI_FCPORTPAGE0_SUPPORT_CLASS_3)
  883. cos |= FC_COS_CLASS3;
  884. fc_host_supported_classes(sh) = cos;
  885. if (pp0->CurrentSpeed == MPI_FCPORTPAGE0_CURRENT_SPEED_1GBIT)
  886. speed = FC_PORTSPEED_1GBIT;
  887. else if (pp0->CurrentSpeed == MPI_FCPORTPAGE0_CURRENT_SPEED_2GBIT)
  888. speed = FC_PORTSPEED_2GBIT;
  889. else if (pp0->CurrentSpeed == MPI_FCPORTPAGE0_CURRENT_SPEED_4GBIT)
  890. speed = FC_PORTSPEED_4GBIT;
  891. else if (pp0->CurrentSpeed == MPI_FCPORTPAGE0_CURRENT_SPEED_10GBIT)
  892. speed = FC_PORTSPEED_10GBIT;
  893. else
  894. speed = FC_PORTSPEED_UNKNOWN;
  895. fc_host_speed(sh) = speed;
  896. speed = 0;
  897. if (pp0->SupportedSpeeds & MPI_FCPORTPAGE0_SUPPORT_1GBIT_SPEED)
  898. speed |= FC_PORTSPEED_1GBIT;
  899. if (pp0->SupportedSpeeds & MPI_FCPORTPAGE0_SUPPORT_2GBIT_SPEED)
  900. speed |= FC_PORTSPEED_2GBIT;
  901. if (pp0->SupportedSpeeds & MPI_FCPORTPAGE0_SUPPORT_4GBIT_SPEED)
  902. speed |= FC_PORTSPEED_4GBIT;
  903. if (pp0->SupportedSpeeds & MPI_FCPORTPAGE0_SUPPORT_10GBIT_SPEED)
  904. speed |= FC_PORTSPEED_10GBIT;
  905. fc_host_supported_speeds(sh) = speed;
  906. port_state = FC_PORTSTATE_UNKNOWN;
  907. if (pp0->PortState == MPI_FCPORTPAGE0_PORTSTATE_ONLINE)
  908. port_state = FC_PORTSTATE_ONLINE;
  909. else if (pp0->PortState == MPI_FCPORTPAGE0_PORTSTATE_OFFLINE)
  910. port_state = FC_PORTSTATE_LINKDOWN;
  911. fc_host_port_state(sh) = port_state;
  912. port_type = FC_PORTTYPE_UNKNOWN;
  913. if (pp0->Flags & MPI_FCPORTPAGE0_FLAGS_ATTACH_POINT_TO_POINT)
  914. port_type = FC_PORTTYPE_PTP;
  915. else if (pp0->Flags & MPI_FCPORTPAGE0_FLAGS_ATTACH_PRIVATE_LOOP)
  916. port_type = FC_PORTTYPE_LPORT;
  917. else if (pp0->Flags & MPI_FCPORTPAGE0_FLAGS_ATTACH_PUBLIC_LOOP)
  918. port_type = FC_PORTTYPE_NLPORT;
  919. else if (pp0->Flags & MPI_FCPORTPAGE0_FLAGS_ATTACH_FABRIC_DIRECT)
  920. port_type = FC_PORTTYPE_NPORT;
  921. fc_host_port_type(sh) = port_type;
  922. fc_host_fabric_name(sh) =
  923. (pp0->Flags & MPI_FCPORTPAGE0_FLAGS_FABRIC_WWN_VALID) ?
  924. (u64) pp0->FabricWWNN.High << 32 | (u64) pp0->FabricWWPN.Low :
  925. (u64)pp0->WWNN.High << 32 | (u64)pp0->WWNN.Low;
  926. }
  927. static void
  928. mptfc_link_status_change(struct work_struct *work)
  929. {
  930. MPT_ADAPTER *ioc =
  931. container_of(work, MPT_ADAPTER, fc_rescan_work);
  932. int ii;
  933. for (ii=0; ii < ioc->facts.NumberOfPorts; ii++)
  934. (void) mptfc_GetFcPortPage0(ioc, ii);
  935. }
  936. static void
  937. mptfc_setup_reset(struct work_struct *work)
  938. {
  939. MPT_ADAPTER *ioc =
  940. container_of(work, MPT_ADAPTER, fc_setup_reset_work);
  941. u64 pn;
  942. struct mptfc_rport_info *ri;
  943. struct scsi_target *starget;
  944. VirtTarget *vtarget;
  945. /* reset about to happen, delete (block) all rports */
  946. list_for_each_entry(ri, &ioc->fc_rports, list) {
  947. if (ri->flags & MPT_RPORT_INFO_FLAGS_REGISTERED) {
  948. ri->flags &= ~MPT_RPORT_INFO_FLAGS_REGISTERED;
  949. fc_remote_port_delete(ri->rport); /* won't sleep */
  950. ri->rport = NULL;
  951. starget = ri->starget;
  952. if (starget) {
  953. vtarget = starget->hostdata;
  954. if (vtarget)
  955. vtarget->deleted = 1;
  956. }
  957. pn = (u64)ri->pg0.WWPN.High << 32 |
  958. (u64)ri->pg0.WWPN.Low;
  959. dfcprintk (ioc, printk(MYIOC_s_DEBUG_FMT
  960. "mptfc_setup_reset.%d: %llx deleted\n",
  961. ioc->name,
  962. ioc->sh->host_no,
  963. (unsigned long long)pn));
  964. }
  965. }
  966. }
  967. static void
  968. mptfc_rescan_devices(struct work_struct *work)
  969. {
  970. MPT_ADAPTER *ioc =
  971. container_of(work, MPT_ADAPTER, fc_rescan_work);
  972. int ii;
  973. u64 pn;
  974. struct mptfc_rport_info *ri;
  975. struct scsi_target *starget;
  976. VirtTarget *vtarget;
  977. /* start by tagging all ports as missing */
  978. list_for_each_entry(ri, &ioc->fc_rports, list) {
  979. if (ri->flags & MPT_RPORT_INFO_FLAGS_REGISTERED) {
  980. ri->flags |= MPT_RPORT_INFO_FLAGS_MISSING;
  981. }
  982. }
  983. /*
  984. * now rescan devices known to adapter,
  985. * will reregister existing rports
  986. */
  987. for (ii=0; ii < ioc->facts.NumberOfPorts; ii++) {
  988. (void) mptfc_GetFcPortPage0(ioc, ii);
  989. mptfc_init_host_attr(ioc, ii); /* refresh */
  990. mptfc_GetFcDevPage0(ioc, ii, mptfc_register_dev);
  991. }
  992. /* delete devices still missing */
  993. list_for_each_entry(ri, &ioc->fc_rports, list) {
  994. /* if newly missing, delete it */
  995. if (ri->flags & MPT_RPORT_INFO_FLAGS_MISSING) {
  996. ri->flags &= ~(MPT_RPORT_INFO_FLAGS_REGISTERED|
  997. MPT_RPORT_INFO_FLAGS_MISSING);
  998. fc_remote_port_delete(ri->rport); /* won't sleep */
  999. ri->rport = NULL;
  1000. starget = ri->starget;
  1001. if (starget) {
  1002. vtarget = starget->hostdata;
  1003. if (vtarget)
  1004. vtarget->deleted = 1;
  1005. }
  1006. pn = (u64)ri->pg0.WWPN.High << 32 |
  1007. (u64)ri->pg0.WWPN.Low;
  1008. dfcprintk (ioc, printk(MYIOC_s_DEBUG_FMT
  1009. "mptfc_rescan.%d: %llx deleted\n",
  1010. ioc->name,
  1011. ioc->sh->host_no,
  1012. (unsigned long long)pn));
  1013. }
  1014. }
  1015. }
  1016. static int
  1017. mptfc_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  1018. {
  1019. struct Scsi_Host *sh;
  1020. MPT_SCSI_HOST *hd;
  1021. MPT_ADAPTER *ioc;
  1022. unsigned long flags;
  1023. int ii;
  1024. int numSGE = 0;
  1025. int scale;
  1026. int ioc_cap;
  1027. int error=0;
  1028. int r;
  1029. if ((r = mpt_attach(pdev,id)) != 0)
  1030. return r;
  1031. ioc = pci_get_drvdata(pdev);
  1032. ioc->DoneCtx = mptfcDoneCtx;
  1033. ioc->TaskCtx = mptfcTaskCtx;
  1034. ioc->InternalCtx = mptfcInternalCtx;
  1035. /* Added sanity check on readiness of the MPT adapter.
  1036. */
  1037. if (ioc->last_state != MPI_IOC_STATE_OPERATIONAL) {
  1038. printk(MYIOC_s_WARN_FMT
  1039. "Skipping because it's not operational!\n",
  1040. ioc->name);
  1041. error = -ENODEV;
  1042. goto out_mptfc_probe;
  1043. }
  1044. if (!ioc->active) {
  1045. printk(MYIOC_s_WARN_FMT "Skipping because it's disabled!\n",
  1046. ioc->name);
  1047. error = -ENODEV;
  1048. goto out_mptfc_probe;
  1049. }
  1050. /* Sanity check - ensure at least 1 port is INITIATOR capable
  1051. */
  1052. ioc_cap = 0;
  1053. for (ii=0; ii < ioc->facts.NumberOfPorts; ii++) {
  1054. if (ioc->pfacts[ii].ProtocolFlags &
  1055. MPI_PORTFACTS_PROTOCOL_INITIATOR)
  1056. ioc_cap ++;
  1057. }
  1058. if (!ioc_cap) {
  1059. printk(MYIOC_s_WARN_FMT
  1060. "Skipping ioc=%p because SCSI Initiator mode is NOT enabled!\n",
  1061. ioc->name, ioc);
  1062. return 0;
  1063. }
  1064. sh = scsi_host_alloc(&mptfc_driver_template, sizeof(MPT_SCSI_HOST));
  1065. if (!sh) {
  1066. printk(MYIOC_s_WARN_FMT
  1067. "Unable to register controller with SCSI subsystem\n",
  1068. ioc->name);
  1069. error = -1;
  1070. goto out_mptfc_probe;
  1071. }
  1072. spin_lock_init(&ioc->fc_rescan_work_lock);
  1073. INIT_WORK(&ioc->fc_rescan_work, mptfc_rescan_devices);
  1074. INIT_WORK(&ioc->fc_setup_reset_work, mptfc_setup_reset);
  1075. INIT_WORK(&ioc->fc_lsc_work, mptfc_link_status_change);
  1076. spin_lock_irqsave(&ioc->FreeQlock, flags);
  1077. /* Attach the SCSI Host to the IOC structure
  1078. */
  1079. ioc->sh = sh;
  1080. sh->io_port = 0;
  1081. sh->n_io_port = 0;
  1082. sh->irq = 0;
  1083. /* set 16 byte cdb's */
  1084. sh->max_cmd_len = 16;
  1085. sh->max_id = ioc->pfacts->MaxDevices;
  1086. sh->max_lun = max_lun;
  1087. /* Required entry.
  1088. */
  1089. sh->unique_id = ioc->id;
  1090. /* Verify that we won't exceed the maximum
  1091. * number of chain buffers
  1092. * We can optimize: ZZ = req_sz/sizeof(SGE)
  1093. * For 32bit SGE's:
  1094. * numSGE = 1 + (ZZ-1)*(maxChain -1) + ZZ
  1095. * + (req_sz - 64)/sizeof(SGE)
  1096. * A slightly different algorithm is required for
  1097. * 64bit SGEs.
  1098. */
  1099. scale = ioc->req_sz/ioc->SGE_size;
  1100. if (ioc->sg_addr_size == sizeof(u64)) {
  1101. numSGE = (scale - 1) *
  1102. (ioc->facts.MaxChainDepth-1) + scale +
  1103. (ioc->req_sz - 60) / ioc->SGE_size;
  1104. } else {
  1105. numSGE = 1 + (scale - 1) *
  1106. (ioc->facts.MaxChainDepth-1) + scale +
  1107. (ioc->req_sz - 64) / ioc->SGE_size;
  1108. }
  1109. if (numSGE < sh->sg_tablesize) {
  1110. /* Reset this value */
  1111. dprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  1112. "Resetting sg_tablesize to %d from %d\n",
  1113. ioc->name, numSGE, sh->sg_tablesize));
  1114. sh->sg_tablesize = numSGE;
  1115. }
  1116. spin_unlock_irqrestore(&ioc->FreeQlock, flags);
  1117. hd = shost_priv(sh);
  1118. hd->ioc = ioc;
  1119. /* SCSI needs scsi_cmnd lookup table!
  1120. * (with size equal to req_depth*PtrSz!)
  1121. */
  1122. ioc->ScsiLookup = kcalloc(ioc->req_depth, sizeof(void *), GFP_ATOMIC);
  1123. if (!ioc->ScsiLookup) {
  1124. error = -ENOMEM;
  1125. goto out_mptfc_probe;
  1126. }
  1127. spin_lock_init(&ioc->scsi_lookup_lock);
  1128. dprintk(ioc, printk(MYIOC_s_DEBUG_FMT "ScsiLookup @ %p\n",
  1129. ioc->name, ioc->ScsiLookup));
  1130. hd->last_queue_full = 0;
  1131. sh->transportt = mptfc_transport_template;
  1132. error = scsi_add_host (sh, &ioc->pcidev->dev);
  1133. if(error) {
  1134. dprintk(ioc, printk(MYIOC_s_ERR_FMT
  1135. "scsi_add_host failed\n", ioc->name));
  1136. goto out_mptfc_probe;
  1137. }
  1138. /* initialize workqueue */
  1139. snprintf(ioc->fc_rescan_work_q_name, sizeof(ioc->fc_rescan_work_q_name),
  1140. "mptfc_wq_%d", sh->host_no);
  1141. ioc->fc_rescan_work_q =
  1142. create_singlethread_workqueue(ioc->fc_rescan_work_q_name);
  1143. if (!ioc->fc_rescan_work_q)
  1144. goto out_mptfc_probe;
  1145. /*
  1146. * Pre-fetch FC port WWN and stuff...
  1147. * (FCPortPage0_t stuff)
  1148. */
  1149. for (ii=0; ii < ioc->facts.NumberOfPorts; ii++) {
  1150. (void) mptfc_GetFcPortPage0(ioc, ii);
  1151. }
  1152. mptfc_SetFcPortPage1_defaults(ioc);
  1153. /*
  1154. * scan for rports -
  1155. * by doing it via the workqueue, some locking is eliminated
  1156. */
  1157. queue_work(ioc->fc_rescan_work_q, &ioc->fc_rescan_work);
  1158. flush_workqueue(ioc->fc_rescan_work_q);
  1159. return 0;
  1160. out_mptfc_probe:
  1161. mptscsih_remove(pdev);
  1162. return error;
  1163. }
  1164. static struct pci_driver mptfc_driver = {
  1165. .name = "mptfc",
  1166. .id_table = mptfc_pci_table,
  1167. .probe = mptfc_probe,
  1168. .remove = mptfc_remove,
  1169. .shutdown = mptscsih_shutdown,
  1170. #ifdef CONFIG_PM
  1171. .suspend = mptscsih_suspend,
  1172. .resume = mptscsih_resume,
  1173. #endif
  1174. };
  1175. static int
  1176. mptfc_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *pEvReply)
  1177. {
  1178. MPT_SCSI_HOST *hd;
  1179. u8 event = le32_to_cpu(pEvReply->Event) & 0xFF;
  1180. unsigned long flags;
  1181. int rc=1;
  1182. if (ioc->bus_type != FC)
  1183. return 0;
  1184. devtverboseprintk(ioc, printk(MYIOC_s_DEBUG_FMT "MPT event (=%02Xh) routed to SCSI host driver!\n",
  1185. ioc->name, event));
  1186. if (ioc->sh == NULL ||
  1187. ((hd = shost_priv(ioc->sh)) == NULL))
  1188. return 1;
  1189. switch (event) {
  1190. case MPI_EVENT_RESCAN:
  1191. spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
  1192. if (ioc->fc_rescan_work_q) {
  1193. queue_work(ioc->fc_rescan_work_q,
  1194. &ioc->fc_rescan_work);
  1195. }
  1196. spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
  1197. break;
  1198. case MPI_EVENT_LINK_STATUS_CHANGE:
  1199. spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
  1200. if (ioc->fc_rescan_work_q) {
  1201. queue_work(ioc->fc_rescan_work_q,
  1202. &ioc->fc_lsc_work);
  1203. }
  1204. spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
  1205. break;
  1206. default:
  1207. rc = mptscsih_event_process(ioc,pEvReply);
  1208. break;
  1209. }
  1210. return rc;
  1211. }
  1212. static int
  1213. mptfc_ioc_reset(MPT_ADAPTER *ioc, int reset_phase)
  1214. {
  1215. int rc;
  1216. unsigned long flags;
  1217. rc = mptscsih_ioc_reset(ioc,reset_phase);
  1218. if ((ioc->bus_type != FC) || (!rc))
  1219. return rc;
  1220. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  1221. ": IOC %s_reset routed to FC host driver!\n",ioc->name,
  1222. reset_phase==MPT_IOC_SETUP_RESET ? "setup" : (
  1223. reset_phase==MPT_IOC_PRE_RESET ? "pre" : "post")));
  1224. if (reset_phase == MPT_IOC_SETUP_RESET) {
  1225. spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
  1226. if (ioc->fc_rescan_work_q) {
  1227. queue_work(ioc->fc_rescan_work_q,
  1228. &ioc->fc_setup_reset_work);
  1229. }
  1230. spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
  1231. }
  1232. else if (reset_phase == MPT_IOC_PRE_RESET) {
  1233. }
  1234. else { /* MPT_IOC_POST_RESET */
  1235. mptfc_SetFcPortPage1_defaults(ioc);
  1236. spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
  1237. if (ioc->fc_rescan_work_q) {
  1238. queue_work(ioc->fc_rescan_work_q,
  1239. &ioc->fc_rescan_work);
  1240. }
  1241. spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
  1242. }
  1243. return 1;
  1244. }
  1245. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1246. /**
  1247. * mptfc_init - Register MPT adapter(s) as SCSI host(s) with SCSI mid-layer.
  1248. *
  1249. * Returns 0 for success, non-zero for failure.
  1250. */
  1251. static int __init
  1252. mptfc_init(void)
  1253. {
  1254. int error;
  1255. show_mptmod_ver(my_NAME, my_VERSION);
  1256. /* sanity check module parameters */
  1257. if (mptfc_dev_loss_tmo <= 0)
  1258. mptfc_dev_loss_tmo = MPTFC_DEV_LOSS_TMO;
  1259. mptfc_transport_template =
  1260. fc_attach_transport(&mptfc_transport_functions);
  1261. if (!mptfc_transport_template)
  1262. return -ENODEV;
  1263. mptfcDoneCtx = mpt_register(mptscsih_io_done, MPTFC_DRIVER,
  1264. "mptscsih_scandv_complete");
  1265. mptfcTaskCtx = mpt_register(mptscsih_taskmgmt_complete, MPTFC_DRIVER,
  1266. "mptscsih_scandv_complete");
  1267. mptfcInternalCtx = mpt_register(mptscsih_scandv_complete, MPTFC_DRIVER,
  1268. "mptscsih_scandv_complete");
  1269. mpt_event_register(mptfcDoneCtx, mptfc_event_process);
  1270. mpt_reset_register(mptfcDoneCtx, mptfc_ioc_reset);
  1271. error = pci_register_driver(&mptfc_driver);
  1272. if (error)
  1273. fc_release_transport(mptfc_transport_template);
  1274. return error;
  1275. }
  1276. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1277. /**
  1278. * mptfc_remove - Remove fc infrastructure for devices
  1279. * @pdev: Pointer to pci_dev structure
  1280. *
  1281. */
  1282. static void mptfc_remove(struct pci_dev *pdev)
  1283. {
  1284. MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
  1285. struct mptfc_rport_info *p, *n;
  1286. struct workqueue_struct *work_q;
  1287. unsigned long flags;
  1288. int ii;
  1289. /* destroy workqueue */
  1290. if ((work_q=ioc->fc_rescan_work_q)) {
  1291. spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
  1292. ioc->fc_rescan_work_q = NULL;
  1293. spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
  1294. destroy_workqueue(work_q);
  1295. }
  1296. fc_remove_host(ioc->sh);
  1297. list_for_each_entry_safe(p, n, &ioc->fc_rports, list) {
  1298. list_del(&p->list);
  1299. kfree(p);
  1300. }
  1301. for (ii=0; ii<ioc->facts.NumberOfPorts; ii++) {
  1302. if (ioc->fc_data.fc_port_page1[ii].data) {
  1303. pci_free_consistent(ioc->pcidev,
  1304. ioc->fc_data.fc_port_page1[ii].pg_sz,
  1305. (u8 *) ioc->fc_data.fc_port_page1[ii].data,
  1306. ioc->fc_data.fc_port_page1[ii].dma);
  1307. ioc->fc_data.fc_port_page1[ii].data = NULL;
  1308. }
  1309. }
  1310. mptscsih_remove(pdev);
  1311. }
  1312. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1313. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1314. /**
  1315. * mptfc_exit - Unregisters MPT adapter(s)
  1316. *
  1317. */
  1318. static void __exit
  1319. mptfc_exit(void)
  1320. {
  1321. pci_unregister_driver(&mptfc_driver);
  1322. fc_release_transport(mptfc_transport_template);
  1323. mpt_reset_deregister(mptfcDoneCtx);
  1324. mpt_event_deregister(mptfcDoneCtx);
  1325. mpt_deregister(mptfcInternalCtx);
  1326. mpt_deregister(mptfcTaskCtx);
  1327. mpt_deregister(mptfcDoneCtx);
  1328. }
  1329. module_init(mptfc_init);
  1330. module_exit(mptfc_exit);