mptsas.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898
  1. /*
  2. * linux/drivers/message/fusion/mptsas.c
  3. * For use with LSI Logic PCI chip/adapter(s)
  4. * running LSI Logic Fusion MPT (Message Passing Technology) firmware.
  5. *
  6. * Copyright (c) 1999-2005 LSI Logic Corporation
  7. * (mailto:mpt_linux_developer@lsil.com)
  8. * Copyright (c) 2005-2006 Dell
  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/sched.h>
  47. #include <linux/workqueue.h>
  48. #include <scsi/scsi_cmnd.h>
  49. #include <scsi/scsi_device.h>
  50. #include <scsi/scsi_host.h>
  51. #include <scsi/scsi_transport_sas.h>
  52. #include "mptbase.h"
  53. #include "mptscsih.h"
  54. #define my_NAME "Fusion MPT SAS Host driver"
  55. #define my_VERSION MPT_LINUX_VERSION_COMMON
  56. #define MYNAM "mptsas"
  57. MODULE_AUTHOR(MODULEAUTHOR);
  58. MODULE_DESCRIPTION(my_NAME);
  59. MODULE_LICENSE("GPL");
  60. static int mpt_pq_filter;
  61. module_param(mpt_pq_filter, int, 0);
  62. MODULE_PARM_DESC(mpt_pq_filter,
  63. "Enable peripheral qualifier filter: enable=1 "
  64. "(default=0)");
  65. static int mpt_pt_clear;
  66. module_param(mpt_pt_clear, int, 0);
  67. MODULE_PARM_DESC(mpt_pt_clear,
  68. "Clear persistency table: enable=1 "
  69. "(default=MPTSCSIH_PT_CLEAR=0)");
  70. static int mptsasDoneCtx = -1;
  71. static int mptsasTaskCtx = -1;
  72. static int mptsasInternalCtx = -1; /* Used only for internal commands */
  73. static int mptsasMgmtCtx = -1;
  74. enum mptsas_hotplug_action {
  75. MPTSAS_ADD_DEVICE,
  76. MPTSAS_DEL_DEVICE,
  77. MPTSAS_ADD_RAID,
  78. MPTSAS_DEL_RAID,
  79. };
  80. struct mptsas_hotplug_event {
  81. struct work_struct work;
  82. MPT_ADAPTER *ioc;
  83. enum mptsas_hotplug_action event_type;
  84. u64 sas_address;
  85. u32 channel;
  86. u32 id;
  87. u32 device_info;
  88. u16 handle;
  89. u16 parent_handle;
  90. u8 phy_id;
  91. };
  92. /*
  93. * SAS topology structures
  94. *
  95. * The MPT Fusion firmware interface spreads information about the
  96. * SAS topology over many manufacture pages, thus we need some data
  97. * structure to collect it and process it for the SAS transport class.
  98. */
  99. struct mptsas_devinfo {
  100. u16 handle; /* unique id to address this device */
  101. u16 handle_parent; /* unique id to address parent device */
  102. u8 phy_id; /* phy number of parent device */
  103. u8 port_id; /* sas physical port this device
  104. is assoc'd with */
  105. u8 id; /* logical target id of this device */
  106. u8 channel; /* logical bus number of this device */
  107. u64 sas_address; /* WWN of this device,
  108. SATA is assigned by HBA,expander */
  109. u32 device_info; /* bitfield detailed info about this device */
  110. };
  111. struct mptsas_phyinfo {
  112. u8 phy_id; /* phy index */
  113. u8 port_id; /* port number this phy is part of */
  114. u8 negotiated_link_rate; /* nego'd link rate for this phy */
  115. u8 hw_link_rate; /* hardware max/min phys link rate */
  116. u8 programmed_link_rate; /* programmed max/min phy link rate */
  117. struct mptsas_devinfo identify; /* point to phy device info */
  118. struct mptsas_devinfo attached; /* point to attached device info */
  119. struct sas_phy *phy;
  120. struct sas_rphy *rphy;
  121. };
  122. struct mptsas_portinfo {
  123. struct list_head list;
  124. u16 handle; /* unique id to address this */
  125. u8 num_phys; /* number of phys */
  126. struct mptsas_phyinfo *phy_info;
  127. };
  128. #ifdef SASDEBUG
  129. static void mptsas_print_phy_data(MPI_SAS_IO_UNIT0_PHY_DATA *phy_data)
  130. {
  131. printk("---- IO UNIT PAGE 0 ------------\n");
  132. printk("Handle=0x%X\n",
  133. le16_to_cpu(phy_data->AttachedDeviceHandle));
  134. printk("Controller Handle=0x%X\n",
  135. le16_to_cpu(phy_data->ControllerDevHandle));
  136. printk("Port=0x%X\n", phy_data->Port);
  137. printk("Port Flags=0x%X\n", phy_data->PortFlags);
  138. printk("PHY Flags=0x%X\n", phy_data->PhyFlags);
  139. printk("Negotiated Link Rate=0x%X\n", phy_data->NegotiatedLinkRate);
  140. printk("Controller PHY Device Info=0x%X\n",
  141. le32_to_cpu(phy_data->ControllerPhyDeviceInfo));
  142. printk("DiscoveryStatus=0x%X\n",
  143. le32_to_cpu(phy_data->DiscoveryStatus));
  144. printk("\n");
  145. }
  146. static void mptsas_print_phy_pg0(SasPhyPage0_t *pg0)
  147. {
  148. __le64 sas_address;
  149. memcpy(&sas_address, &pg0->SASAddress, sizeof(__le64));
  150. printk("---- SAS PHY PAGE 0 ------------\n");
  151. printk("Attached Device Handle=0x%X\n",
  152. le16_to_cpu(pg0->AttachedDevHandle));
  153. printk("SAS Address=0x%llX\n",
  154. (unsigned long long)le64_to_cpu(sas_address));
  155. printk("Attached PHY Identifier=0x%X\n", pg0->AttachedPhyIdentifier);
  156. printk("Attached Device Info=0x%X\n",
  157. le32_to_cpu(pg0->AttachedDeviceInfo));
  158. printk("Programmed Link Rate=0x%X\n", pg0->ProgrammedLinkRate);
  159. printk("Change Count=0x%X\n", pg0->ChangeCount);
  160. printk("PHY Info=0x%X\n", le32_to_cpu(pg0->PhyInfo));
  161. printk("\n");
  162. }
  163. static void mptsas_print_phy_pg1(SasPhyPage1_t *pg1)
  164. {
  165. printk("---- SAS PHY PAGE 1 ------------\n");
  166. printk("Invalid Dword Count=0x%x\n", pg1->InvalidDwordCount);
  167. printk("Running Disparity Error Count=0x%x\n",
  168. pg1->RunningDisparityErrorCount);
  169. printk("Loss Dword Synch Count=0x%x\n", pg1->LossDwordSynchCount);
  170. printk("PHY Reset Problem Count=0x%x\n", pg1->PhyResetProblemCount);
  171. printk("\n");
  172. }
  173. static void mptsas_print_device_pg0(SasDevicePage0_t *pg0)
  174. {
  175. __le64 sas_address;
  176. memcpy(&sas_address, &pg0->SASAddress, sizeof(__le64));
  177. printk("---- SAS DEVICE PAGE 0 ---------\n");
  178. printk("Handle=0x%X\n" ,le16_to_cpu(pg0->DevHandle));
  179. printk("Enclosure Handle=0x%X\n", le16_to_cpu(pg0->EnclosureHandle));
  180. printk("Slot=0x%X\n", le16_to_cpu(pg0->Slot));
  181. printk("SAS Address=0x%llX\n", le64_to_cpu(sas_address));
  182. printk("Target ID=0x%X\n", pg0->TargetID);
  183. printk("Bus=0x%X\n", pg0->Bus);
  184. /* The PhyNum field specifies the PHY number of the parent
  185. * device this device is linked to
  186. */
  187. printk("Parent Phy Num=0x%X\n", pg0->PhyNum);
  188. printk("Access Status=0x%X\n", le16_to_cpu(pg0->AccessStatus));
  189. printk("Device Info=0x%X\n", le32_to_cpu(pg0->DeviceInfo));
  190. printk("Flags=0x%X\n", le16_to_cpu(pg0->Flags));
  191. printk("Physical Port=0x%X\n", pg0->PhysicalPort);
  192. printk("\n");
  193. }
  194. static void mptsas_print_expander_pg1(SasExpanderPage1_t *pg1)
  195. {
  196. printk("---- SAS EXPANDER PAGE 1 ------------\n");
  197. printk("Physical Port=0x%X\n", pg1->PhysicalPort);
  198. printk("PHY Identifier=0x%X\n", pg1->PhyIdentifier);
  199. printk("Negotiated Link Rate=0x%X\n", pg1->NegotiatedLinkRate);
  200. printk("Programmed Link Rate=0x%X\n", pg1->ProgrammedLinkRate);
  201. printk("Hardware Link Rate=0x%X\n", pg1->HwLinkRate);
  202. printk("Owner Device Handle=0x%X\n",
  203. le16_to_cpu(pg1->OwnerDevHandle));
  204. printk("Attached Device Handle=0x%X\n",
  205. le16_to_cpu(pg1->AttachedDevHandle));
  206. }
  207. #else
  208. #define mptsas_print_phy_data(phy_data) do { } while (0)
  209. #define mptsas_print_phy_pg0(pg0) do { } while (0)
  210. #define mptsas_print_phy_pg1(pg1) do { } while (0)
  211. #define mptsas_print_device_pg0(pg0) do { } while (0)
  212. #define mptsas_print_expander_pg1(pg1) do { } while (0)
  213. #endif
  214. /*
  215. * This is pretty ugly. We will be able to seriously clean it up
  216. * once the DV code in mptscsih goes away and we can properly
  217. * implement ->target_alloc.
  218. */
  219. static int
  220. mptsas_slave_alloc(struct scsi_device *sdev)
  221. {
  222. struct Scsi_Host *host = sdev->host;
  223. MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)host->hostdata;
  224. struct sas_rphy *rphy;
  225. struct mptsas_portinfo *p;
  226. VirtTarget *vtarget;
  227. VirtDevice *vdev;
  228. struct scsi_target *starget;
  229. int i;
  230. vdev = kzalloc(sizeof(VirtDevice), GFP_KERNEL);
  231. if (!vdev) {
  232. printk(MYIOC_s_ERR_FMT "slave_alloc kmalloc(%zd) FAILED!\n",
  233. hd->ioc->name, sizeof(VirtDevice));
  234. return -ENOMEM;
  235. }
  236. vdev->ioc_id = hd->ioc->id;
  237. sdev->hostdata = vdev;
  238. starget = scsi_target(sdev);
  239. vtarget = starget->hostdata;
  240. vdev->vtarget = vtarget;
  241. if (vtarget->num_luns == 0) {
  242. vtarget->tflags = MPT_TARGET_FLAGS_Q_YES|MPT_TARGET_FLAGS_VALID_INQUIRY;
  243. hd->Targets[sdev->id] = vtarget;
  244. }
  245. /*
  246. RAID volumes placed beyond the last expected port.
  247. */
  248. if (sdev->channel == hd->ioc->num_ports) {
  249. vdev->target_id = sdev->id;
  250. vdev->bus_id = 0;
  251. vdev->lun = 0;
  252. goto out;
  253. }
  254. rphy = dev_to_rphy(sdev->sdev_target->dev.parent);
  255. mutex_lock(&hd->ioc->sas_topology_mutex);
  256. list_for_each_entry(p, &hd->ioc->sas_topology, list) {
  257. for (i = 0; i < p->num_phys; i++) {
  258. if (p->phy_info[i].attached.sas_address ==
  259. rphy->identify.sas_address) {
  260. vdev->target_id =
  261. p->phy_info[i].attached.id;
  262. vdev->bus_id = p->phy_info[i].attached.channel;
  263. vdev->lun = sdev->lun;
  264. mutex_unlock(&hd->ioc->sas_topology_mutex);
  265. goto out;
  266. }
  267. }
  268. }
  269. mutex_unlock(&hd->ioc->sas_topology_mutex);
  270. kfree(vdev);
  271. return -ENXIO;
  272. out:
  273. vtarget->ioc_id = vdev->ioc_id;
  274. vtarget->target_id = vdev->target_id;
  275. vtarget->bus_id = vdev->bus_id;
  276. vtarget->num_luns++;
  277. return 0;
  278. }
  279. static void
  280. mptsas_slave_destroy(struct scsi_device *sdev)
  281. {
  282. struct Scsi_Host *host = sdev->host;
  283. MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)host->hostdata;
  284. struct sas_rphy *rphy;
  285. struct mptsas_portinfo *p;
  286. int i;
  287. VirtDevice *vdev;
  288. /*
  289. * Handle hotplug removal case.
  290. * We need to clear out attached data structure.
  291. */
  292. rphy = dev_to_rphy(sdev->sdev_target->dev.parent);
  293. mutex_lock(&hd->ioc->sas_topology_mutex);
  294. list_for_each_entry(p, &hd->ioc->sas_topology, list) {
  295. for (i = 0; i < p->num_phys; i++) {
  296. if (p->phy_info[i].attached.sas_address ==
  297. rphy->identify.sas_address) {
  298. memset(&p->phy_info[i].attached, 0,
  299. sizeof(struct mptsas_devinfo));
  300. p->phy_info[i].rphy = NULL;
  301. goto out;
  302. }
  303. }
  304. }
  305. out:
  306. mutex_unlock(&hd->ioc->sas_topology_mutex);
  307. /*
  308. * Issue target reset to flush firmware outstanding commands.
  309. */
  310. vdev = sdev->hostdata;
  311. if (vdev->configured_lun){
  312. if (mptscsih_TMHandler(hd,
  313. MPI_SCSITASKMGMT_TASKTYPE_TARGET_RESET,
  314. vdev->bus_id,
  315. vdev->target_id,
  316. 0, 0, 5 /* 5 second timeout */)
  317. < 0){
  318. /* The TM request failed!
  319. * Fatal error case.
  320. */
  321. printk(MYIOC_s_WARN_FMT
  322. "Error processing TaskMgmt id=%d TARGET_RESET\n",
  323. hd->ioc->name,
  324. vdev->target_id);
  325. hd->tmPending = 0;
  326. hd->tmState = TM_STATE_NONE;
  327. }
  328. }
  329. mptscsih_slave_destroy(sdev);
  330. }
  331. static struct scsi_host_template mptsas_driver_template = {
  332. .module = THIS_MODULE,
  333. .proc_name = "mptsas",
  334. .proc_info = mptscsih_proc_info,
  335. .name = "MPT SPI Host",
  336. .info = mptscsih_info,
  337. .queuecommand = mptscsih_qcmd,
  338. .target_alloc = mptscsih_target_alloc,
  339. .slave_alloc = mptsas_slave_alloc,
  340. .slave_configure = mptscsih_slave_configure,
  341. .target_destroy = mptscsih_target_destroy,
  342. .slave_destroy = mptsas_slave_destroy,
  343. .change_queue_depth = mptscsih_change_queue_depth,
  344. .eh_abort_handler = mptscsih_abort,
  345. .eh_device_reset_handler = mptscsih_dev_reset,
  346. .eh_bus_reset_handler = mptscsih_bus_reset,
  347. .eh_host_reset_handler = mptscsih_host_reset,
  348. .bios_param = mptscsih_bios_param,
  349. .can_queue = MPT_FC_CAN_QUEUE,
  350. .this_id = -1,
  351. .sg_tablesize = MPT_SCSI_SG_DEPTH,
  352. .max_sectors = 8192,
  353. .cmd_per_lun = 7,
  354. .use_clustering = ENABLE_CLUSTERING,
  355. };
  356. static inline MPT_ADAPTER *phy_to_ioc(struct sas_phy *phy)
  357. {
  358. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  359. return ((MPT_SCSI_HOST *)shost->hostdata)->ioc;
  360. }
  361. static int mptsas_get_linkerrors(struct sas_phy *phy)
  362. {
  363. MPT_ADAPTER *ioc = phy_to_ioc(phy);
  364. ConfigExtendedPageHeader_t hdr;
  365. CONFIGPARMS cfg;
  366. SasPhyPage1_t *buffer;
  367. dma_addr_t dma_handle;
  368. int error;
  369. hdr.PageVersion = MPI_SASPHY1_PAGEVERSION;
  370. hdr.ExtPageLength = 0;
  371. hdr.PageNumber = 1 /* page number 1*/;
  372. hdr.Reserved1 = 0;
  373. hdr.Reserved2 = 0;
  374. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  375. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_PHY;
  376. cfg.cfghdr.ehdr = &hdr;
  377. cfg.physAddr = -1;
  378. cfg.pageAddr = phy->identify.phy_identifier;
  379. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  380. cfg.dir = 0; /* read */
  381. cfg.timeout = 10;
  382. error = mpt_config(ioc, &cfg);
  383. if (error)
  384. return error;
  385. if (!hdr.ExtPageLength)
  386. return -ENXIO;
  387. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  388. &dma_handle);
  389. if (!buffer)
  390. return -ENOMEM;
  391. cfg.physAddr = dma_handle;
  392. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  393. error = mpt_config(ioc, &cfg);
  394. if (error)
  395. goto out_free_consistent;
  396. mptsas_print_phy_pg1(buffer);
  397. phy->invalid_dword_count = le32_to_cpu(buffer->InvalidDwordCount);
  398. phy->running_disparity_error_count =
  399. le32_to_cpu(buffer->RunningDisparityErrorCount);
  400. phy->loss_of_dword_sync_count =
  401. le32_to_cpu(buffer->LossDwordSynchCount);
  402. phy->phy_reset_problem_count =
  403. le32_to_cpu(buffer->PhyResetProblemCount);
  404. out_free_consistent:
  405. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  406. buffer, dma_handle);
  407. return error;
  408. }
  409. static int mptsas_mgmt_done(MPT_ADAPTER *ioc, MPT_FRAME_HDR *req,
  410. MPT_FRAME_HDR *reply)
  411. {
  412. ioc->sas_mgmt.status |= MPT_SAS_MGMT_STATUS_COMMAND_GOOD;
  413. if (reply != NULL) {
  414. ioc->sas_mgmt.status |= MPT_SAS_MGMT_STATUS_RF_VALID;
  415. memcpy(ioc->sas_mgmt.reply, reply,
  416. min(ioc->reply_sz, 4 * reply->u.reply.MsgLength));
  417. }
  418. complete(&ioc->sas_mgmt.done);
  419. return 1;
  420. }
  421. static int mptsas_phy_reset(struct sas_phy *phy, int hard_reset)
  422. {
  423. MPT_ADAPTER *ioc = phy_to_ioc(phy);
  424. SasIoUnitControlRequest_t *req;
  425. SasIoUnitControlReply_t *reply;
  426. MPT_FRAME_HDR *mf;
  427. MPIHeader_t *hdr;
  428. unsigned long timeleft;
  429. int error = -ERESTARTSYS;
  430. /* not implemented for expanders */
  431. if (phy->identify.target_port_protocols & SAS_PROTOCOL_SMP)
  432. return -ENXIO;
  433. if (mutex_lock_interruptible(&ioc->sas_mgmt.mutex))
  434. goto out;
  435. mf = mpt_get_msg_frame(mptsasMgmtCtx, ioc);
  436. if (!mf) {
  437. error = -ENOMEM;
  438. goto out_unlock;
  439. }
  440. hdr = (MPIHeader_t *) mf;
  441. req = (SasIoUnitControlRequest_t *)mf;
  442. memset(req, 0, sizeof(SasIoUnitControlRequest_t));
  443. req->Function = MPI_FUNCTION_SAS_IO_UNIT_CONTROL;
  444. req->MsgContext = hdr->MsgContext;
  445. req->Operation = hard_reset ?
  446. MPI_SAS_OP_PHY_HARD_RESET : MPI_SAS_OP_PHY_LINK_RESET;
  447. req->PhyNum = phy->identify.phy_identifier;
  448. mpt_put_msg_frame(mptsasMgmtCtx, ioc, mf);
  449. timeleft = wait_for_completion_timeout(&ioc->sas_mgmt.done,
  450. 10 * HZ);
  451. if (!timeleft) {
  452. /* On timeout reset the board */
  453. mpt_free_msg_frame(ioc, mf);
  454. mpt_HardResetHandler(ioc, CAN_SLEEP);
  455. error = -ETIMEDOUT;
  456. goto out_unlock;
  457. }
  458. /* a reply frame is expected */
  459. if ((ioc->sas_mgmt.status &
  460. MPT_IOCTL_STATUS_RF_VALID) == 0) {
  461. error = -ENXIO;
  462. goto out_unlock;
  463. }
  464. /* process the completed Reply Message Frame */
  465. reply = (SasIoUnitControlReply_t *)ioc->sas_mgmt.reply;
  466. if (reply->IOCStatus != MPI_IOCSTATUS_SUCCESS) {
  467. printk("%s: IOCStatus=0x%X IOCLogInfo=0x%X\n",
  468. __FUNCTION__,
  469. reply->IOCStatus,
  470. reply->IOCLogInfo);
  471. error = -ENXIO;
  472. goto out_unlock;
  473. }
  474. error = 0;
  475. out_unlock:
  476. mutex_unlock(&ioc->sas_mgmt.mutex);
  477. out:
  478. return error;
  479. }
  480. static struct sas_function_template mptsas_transport_functions = {
  481. .get_linkerrors = mptsas_get_linkerrors,
  482. .phy_reset = mptsas_phy_reset,
  483. };
  484. static struct scsi_transport_template *mptsas_transport_template;
  485. static int
  486. mptsas_sas_io_unit_pg0(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info)
  487. {
  488. ConfigExtendedPageHeader_t hdr;
  489. CONFIGPARMS cfg;
  490. SasIOUnitPage0_t *buffer;
  491. dma_addr_t dma_handle;
  492. int error, i;
  493. hdr.PageVersion = MPI_SASIOUNITPAGE0_PAGEVERSION;
  494. hdr.ExtPageLength = 0;
  495. hdr.PageNumber = 0;
  496. hdr.Reserved1 = 0;
  497. hdr.Reserved2 = 0;
  498. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  499. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_IO_UNIT;
  500. cfg.cfghdr.ehdr = &hdr;
  501. cfg.physAddr = -1;
  502. cfg.pageAddr = 0;
  503. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  504. cfg.dir = 0; /* read */
  505. cfg.timeout = 10;
  506. error = mpt_config(ioc, &cfg);
  507. if (error)
  508. goto out;
  509. if (!hdr.ExtPageLength) {
  510. error = -ENXIO;
  511. goto out;
  512. }
  513. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  514. &dma_handle);
  515. if (!buffer) {
  516. error = -ENOMEM;
  517. goto out;
  518. }
  519. cfg.physAddr = dma_handle;
  520. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  521. error = mpt_config(ioc, &cfg);
  522. if (error)
  523. goto out_free_consistent;
  524. port_info->num_phys = buffer->NumPhys;
  525. port_info->phy_info = kcalloc(port_info->num_phys,
  526. sizeof(struct mptsas_phyinfo),GFP_KERNEL);
  527. if (!port_info->phy_info) {
  528. error = -ENOMEM;
  529. goto out_free_consistent;
  530. }
  531. for (i = 0; i < port_info->num_phys; i++) {
  532. mptsas_print_phy_data(&buffer->PhyData[i]);
  533. port_info->phy_info[i].phy_id = i;
  534. port_info->phy_info[i].port_id =
  535. buffer->PhyData[i].Port;
  536. port_info->phy_info[i].negotiated_link_rate =
  537. buffer->PhyData[i].NegotiatedLinkRate;
  538. }
  539. out_free_consistent:
  540. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  541. buffer, dma_handle);
  542. out:
  543. return error;
  544. }
  545. static int
  546. mptsas_sas_phy_pg0(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info,
  547. u32 form, u32 form_specific)
  548. {
  549. ConfigExtendedPageHeader_t hdr;
  550. CONFIGPARMS cfg;
  551. SasPhyPage0_t *buffer;
  552. dma_addr_t dma_handle;
  553. int error;
  554. hdr.PageVersion = MPI_SASPHY0_PAGEVERSION;
  555. hdr.ExtPageLength = 0;
  556. hdr.PageNumber = 0;
  557. hdr.Reserved1 = 0;
  558. hdr.Reserved2 = 0;
  559. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  560. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_PHY;
  561. cfg.cfghdr.ehdr = &hdr;
  562. cfg.dir = 0; /* read */
  563. cfg.timeout = 10;
  564. /* Get Phy Pg 0 for each Phy. */
  565. cfg.physAddr = -1;
  566. cfg.pageAddr = form + form_specific;
  567. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  568. error = mpt_config(ioc, &cfg);
  569. if (error)
  570. goto out;
  571. if (!hdr.ExtPageLength) {
  572. error = -ENXIO;
  573. goto out;
  574. }
  575. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  576. &dma_handle);
  577. if (!buffer) {
  578. error = -ENOMEM;
  579. goto out;
  580. }
  581. cfg.physAddr = dma_handle;
  582. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  583. error = mpt_config(ioc, &cfg);
  584. if (error)
  585. goto out_free_consistent;
  586. mptsas_print_phy_pg0(buffer);
  587. phy_info->hw_link_rate = buffer->HwLinkRate;
  588. phy_info->programmed_link_rate = buffer->ProgrammedLinkRate;
  589. phy_info->identify.handle = le16_to_cpu(buffer->OwnerDevHandle);
  590. phy_info->attached.handle = le16_to_cpu(buffer->AttachedDevHandle);
  591. out_free_consistent:
  592. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  593. buffer, dma_handle);
  594. out:
  595. return error;
  596. }
  597. static int
  598. mptsas_sas_device_pg0(MPT_ADAPTER *ioc, struct mptsas_devinfo *device_info,
  599. u32 form, u32 form_specific)
  600. {
  601. ConfigExtendedPageHeader_t hdr;
  602. CONFIGPARMS cfg;
  603. SasDevicePage0_t *buffer;
  604. dma_addr_t dma_handle;
  605. __le64 sas_address;
  606. int error;
  607. hdr.PageVersion = MPI_SASDEVICE0_PAGEVERSION;
  608. hdr.ExtPageLength = 0;
  609. hdr.PageNumber = 0;
  610. hdr.Reserved1 = 0;
  611. hdr.Reserved2 = 0;
  612. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  613. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_DEVICE;
  614. cfg.cfghdr.ehdr = &hdr;
  615. cfg.pageAddr = form + form_specific;
  616. cfg.physAddr = -1;
  617. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  618. cfg.dir = 0; /* read */
  619. cfg.timeout = 10;
  620. error = mpt_config(ioc, &cfg);
  621. if (error)
  622. goto out;
  623. if (!hdr.ExtPageLength) {
  624. error = -ENXIO;
  625. goto out;
  626. }
  627. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  628. &dma_handle);
  629. if (!buffer) {
  630. error = -ENOMEM;
  631. goto out;
  632. }
  633. cfg.physAddr = dma_handle;
  634. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  635. error = mpt_config(ioc, &cfg);
  636. if (error)
  637. goto out_free_consistent;
  638. mptsas_print_device_pg0(buffer);
  639. device_info->handle = le16_to_cpu(buffer->DevHandle);
  640. device_info->handle_parent = le16_to_cpu(buffer->ParentDevHandle);
  641. device_info->phy_id = buffer->PhyNum;
  642. device_info->port_id = buffer->PhysicalPort;
  643. device_info->id = buffer->TargetID;
  644. device_info->channel = buffer->Bus;
  645. memcpy(&sas_address, &buffer->SASAddress, sizeof(__le64));
  646. device_info->sas_address = le64_to_cpu(sas_address);
  647. device_info->device_info =
  648. le32_to_cpu(buffer->DeviceInfo);
  649. out_free_consistent:
  650. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  651. buffer, dma_handle);
  652. out:
  653. return error;
  654. }
  655. static int
  656. mptsas_sas_expander_pg0(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info,
  657. u32 form, u32 form_specific)
  658. {
  659. ConfigExtendedPageHeader_t hdr;
  660. CONFIGPARMS cfg;
  661. SasExpanderPage0_t *buffer;
  662. dma_addr_t dma_handle;
  663. int error;
  664. hdr.PageVersion = MPI_SASEXPANDER0_PAGEVERSION;
  665. hdr.ExtPageLength = 0;
  666. hdr.PageNumber = 0;
  667. hdr.Reserved1 = 0;
  668. hdr.Reserved2 = 0;
  669. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  670. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_EXPANDER;
  671. cfg.cfghdr.ehdr = &hdr;
  672. cfg.physAddr = -1;
  673. cfg.pageAddr = form + form_specific;
  674. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  675. cfg.dir = 0; /* read */
  676. cfg.timeout = 10;
  677. error = mpt_config(ioc, &cfg);
  678. if (error)
  679. goto out;
  680. if (!hdr.ExtPageLength) {
  681. error = -ENXIO;
  682. goto out;
  683. }
  684. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  685. &dma_handle);
  686. if (!buffer) {
  687. error = -ENOMEM;
  688. goto out;
  689. }
  690. cfg.physAddr = dma_handle;
  691. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  692. error = mpt_config(ioc, &cfg);
  693. if (error)
  694. goto out_free_consistent;
  695. /* save config data */
  696. port_info->num_phys = buffer->NumPhys;
  697. port_info->handle = le16_to_cpu(buffer->DevHandle);
  698. port_info->phy_info = kcalloc(port_info->num_phys,
  699. sizeof(struct mptsas_phyinfo),GFP_KERNEL);
  700. if (!port_info->phy_info) {
  701. error = -ENOMEM;
  702. goto out_free_consistent;
  703. }
  704. out_free_consistent:
  705. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  706. buffer, dma_handle);
  707. out:
  708. return error;
  709. }
  710. static int
  711. mptsas_sas_expander_pg1(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info,
  712. u32 form, u32 form_specific)
  713. {
  714. ConfigExtendedPageHeader_t hdr;
  715. CONFIGPARMS cfg;
  716. SasExpanderPage1_t *buffer;
  717. dma_addr_t dma_handle;
  718. int error;
  719. hdr.PageVersion = MPI_SASEXPANDER0_PAGEVERSION;
  720. hdr.ExtPageLength = 0;
  721. hdr.PageNumber = 1;
  722. hdr.Reserved1 = 0;
  723. hdr.Reserved2 = 0;
  724. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  725. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_EXPANDER;
  726. cfg.cfghdr.ehdr = &hdr;
  727. cfg.physAddr = -1;
  728. cfg.pageAddr = form + form_specific;
  729. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  730. cfg.dir = 0; /* read */
  731. cfg.timeout = 10;
  732. error = mpt_config(ioc, &cfg);
  733. if (error)
  734. goto out;
  735. if (!hdr.ExtPageLength) {
  736. error = -ENXIO;
  737. goto out;
  738. }
  739. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  740. &dma_handle);
  741. if (!buffer) {
  742. error = -ENOMEM;
  743. goto out;
  744. }
  745. cfg.physAddr = dma_handle;
  746. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  747. error = mpt_config(ioc, &cfg);
  748. if (error)
  749. goto out_free_consistent;
  750. mptsas_print_expander_pg1(buffer);
  751. /* save config data */
  752. phy_info->phy_id = buffer->PhyIdentifier;
  753. phy_info->port_id = buffer->PhysicalPort;
  754. phy_info->negotiated_link_rate = buffer->NegotiatedLinkRate;
  755. phy_info->programmed_link_rate = buffer->ProgrammedLinkRate;
  756. phy_info->hw_link_rate = buffer->HwLinkRate;
  757. phy_info->identify.handle = le16_to_cpu(buffer->OwnerDevHandle);
  758. phy_info->attached.handle = le16_to_cpu(buffer->AttachedDevHandle);
  759. out_free_consistent:
  760. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  761. buffer, dma_handle);
  762. out:
  763. return error;
  764. }
  765. /*
  766. * Returns true if there is a scsi end device
  767. */
  768. static inline int
  769. mptsas_is_end_device(struct mptsas_devinfo * attached)
  770. {
  771. if ((attached->handle) &&
  772. (attached->device_info &
  773. MPI_SAS_DEVICE_INFO_END_DEVICE) &&
  774. ((attached->device_info &
  775. MPI_SAS_DEVICE_INFO_SSP_TARGET) |
  776. (attached->device_info &
  777. MPI_SAS_DEVICE_INFO_STP_TARGET) |
  778. (attached->device_info &
  779. MPI_SAS_DEVICE_INFO_SATA_DEVICE)))
  780. return 1;
  781. else
  782. return 0;
  783. }
  784. static void
  785. mptsas_parse_device_info(struct sas_identify *identify,
  786. struct mptsas_devinfo *device_info)
  787. {
  788. u16 protocols;
  789. identify->sas_address = device_info->sas_address;
  790. identify->phy_identifier = device_info->phy_id;
  791. /*
  792. * Fill in Phy Initiator Port Protocol.
  793. * Bits 6:3, more than one bit can be set, fall through cases.
  794. */
  795. protocols = device_info->device_info & 0x78;
  796. identify->initiator_port_protocols = 0;
  797. if (protocols & MPI_SAS_DEVICE_INFO_SSP_INITIATOR)
  798. identify->initiator_port_protocols |= SAS_PROTOCOL_SSP;
  799. if (protocols & MPI_SAS_DEVICE_INFO_STP_INITIATOR)
  800. identify->initiator_port_protocols |= SAS_PROTOCOL_STP;
  801. if (protocols & MPI_SAS_DEVICE_INFO_SMP_INITIATOR)
  802. identify->initiator_port_protocols |= SAS_PROTOCOL_SMP;
  803. if (protocols & MPI_SAS_DEVICE_INFO_SATA_HOST)
  804. identify->initiator_port_protocols |= SAS_PROTOCOL_SATA;
  805. /*
  806. * Fill in Phy Target Port Protocol.
  807. * Bits 10:7, more than one bit can be set, fall through cases.
  808. */
  809. protocols = device_info->device_info & 0x780;
  810. identify->target_port_protocols = 0;
  811. if (protocols & MPI_SAS_DEVICE_INFO_SSP_TARGET)
  812. identify->target_port_protocols |= SAS_PROTOCOL_SSP;
  813. if (protocols & MPI_SAS_DEVICE_INFO_STP_TARGET)
  814. identify->target_port_protocols |= SAS_PROTOCOL_STP;
  815. if (protocols & MPI_SAS_DEVICE_INFO_SMP_TARGET)
  816. identify->target_port_protocols |= SAS_PROTOCOL_SMP;
  817. if (protocols & MPI_SAS_DEVICE_INFO_SATA_DEVICE)
  818. identify->target_port_protocols |= SAS_PROTOCOL_SATA;
  819. /*
  820. * Fill in Attached device type.
  821. */
  822. switch (device_info->device_info &
  823. MPI_SAS_DEVICE_INFO_MASK_DEVICE_TYPE) {
  824. case MPI_SAS_DEVICE_INFO_NO_DEVICE:
  825. identify->device_type = SAS_PHY_UNUSED;
  826. break;
  827. case MPI_SAS_DEVICE_INFO_END_DEVICE:
  828. identify->device_type = SAS_END_DEVICE;
  829. break;
  830. case MPI_SAS_DEVICE_INFO_EDGE_EXPANDER:
  831. identify->device_type = SAS_EDGE_EXPANDER_DEVICE;
  832. break;
  833. case MPI_SAS_DEVICE_INFO_FANOUT_EXPANDER:
  834. identify->device_type = SAS_FANOUT_EXPANDER_DEVICE;
  835. break;
  836. }
  837. }
  838. static int mptsas_probe_one_phy(struct device *dev,
  839. struct mptsas_phyinfo *phy_info, int index, int local)
  840. {
  841. struct sas_phy *phy;
  842. int error;
  843. phy = sas_phy_alloc(dev, index);
  844. if (!phy)
  845. return -ENOMEM;
  846. phy->port_identifier = phy_info->port_id;
  847. mptsas_parse_device_info(&phy->identify, &phy_info->identify);
  848. /*
  849. * Set Negotiated link rate.
  850. */
  851. switch (phy_info->negotiated_link_rate) {
  852. case MPI_SAS_IOUNIT0_RATE_PHY_DISABLED:
  853. phy->negotiated_linkrate = SAS_PHY_DISABLED;
  854. break;
  855. case MPI_SAS_IOUNIT0_RATE_FAILED_SPEED_NEGOTIATION:
  856. phy->negotiated_linkrate = SAS_LINK_RATE_FAILED;
  857. break;
  858. case MPI_SAS_IOUNIT0_RATE_1_5:
  859. phy->negotiated_linkrate = SAS_LINK_RATE_1_5_GBPS;
  860. break;
  861. case MPI_SAS_IOUNIT0_RATE_3_0:
  862. phy->negotiated_linkrate = SAS_LINK_RATE_3_0_GBPS;
  863. break;
  864. case MPI_SAS_IOUNIT0_RATE_SATA_OOB_COMPLETE:
  865. case MPI_SAS_IOUNIT0_RATE_UNKNOWN:
  866. default:
  867. phy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
  868. break;
  869. }
  870. /*
  871. * Set Max hardware link rate.
  872. */
  873. switch (phy_info->hw_link_rate & MPI_SAS_PHY0_PRATE_MAX_RATE_MASK) {
  874. case MPI_SAS_PHY0_HWRATE_MAX_RATE_1_5:
  875. phy->maximum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
  876. break;
  877. case MPI_SAS_PHY0_PRATE_MAX_RATE_3_0:
  878. phy->maximum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
  879. break;
  880. default:
  881. break;
  882. }
  883. /*
  884. * Set Max programmed link rate.
  885. */
  886. switch (phy_info->programmed_link_rate &
  887. MPI_SAS_PHY0_PRATE_MAX_RATE_MASK) {
  888. case MPI_SAS_PHY0_PRATE_MAX_RATE_1_5:
  889. phy->maximum_linkrate = SAS_LINK_RATE_1_5_GBPS;
  890. break;
  891. case MPI_SAS_PHY0_PRATE_MAX_RATE_3_0:
  892. phy->maximum_linkrate = SAS_LINK_RATE_3_0_GBPS;
  893. break;
  894. default:
  895. break;
  896. }
  897. /*
  898. * Set Min hardware link rate.
  899. */
  900. switch (phy_info->hw_link_rate & MPI_SAS_PHY0_HWRATE_MIN_RATE_MASK) {
  901. case MPI_SAS_PHY0_HWRATE_MIN_RATE_1_5:
  902. phy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
  903. break;
  904. case MPI_SAS_PHY0_PRATE_MIN_RATE_3_0:
  905. phy->minimum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
  906. break;
  907. default:
  908. break;
  909. }
  910. /*
  911. * Set Min programmed link rate.
  912. */
  913. switch (phy_info->programmed_link_rate &
  914. MPI_SAS_PHY0_PRATE_MIN_RATE_MASK) {
  915. case MPI_SAS_PHY0_PRATE_MIN_RATE_1_5:
  916. phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
  917. break;
  918. case MPI_SAS_PHY0_PRATE_MIN_RATE_3_0:
  919. phy->minimum_linkrate = SAS_LINK_RATE_3_0_GBPS;
  920. break;
  921. default:
  922. break;
  923. }
  924. if (local)
  925. phy->local_attached = 1;
  926. error = sas_phy_add(phy);
  927. if (error) {
  928. sas_phy_free(phy);
  929. return error;
  930. }
  931. phy_info->phy = phy;
  932. if (phy_info->attached.handle) {
  933. struct sas_rphy *rphy;
  934. rphy = sas_rphy_alloc(phy);
  935. if (!rphy)
  936. return 0; /* non-fatal: an rphy can be added later */
  937. mptsas_parse_device_info(&rphy->identify, &phy_info->attached);
  938. error = sas_rphy_add(rphy);
  939. if (error) {
  940. sas_rphy_free(rphy);
  941. return error;
  942. }
  943. phy_info->rphy = rphy;
  944. }
  945. return 0;
  946. }
  947. static int
  948. mptsas_probe_hba_phys(MPT_ADAPTER *ioc, int *index)
  949. {
  950. struct mptsas_portinfo *port_info;
  951. u32 handle = 0xFFFF;
  952. int error = -ENOMEM, i;
  953. port_info = kzalloc(sizeof(*port_info), GFP_KERNEL);
  954. if (!port_info)
  955. goto out;
  956. error = mptsas_sas_io_unit_pg0(ioc, port_info);
  957. if (error)
  958. goto out_free_port_info;
  959. ioc->num_ports = port_info->num_phys;
  960. mutex_lock(&ioc->sas_topology_mutex);
  961. list_add_tail(&port_info->list, &ioc->sas_topology);
  962. mutex_unlock(&ioc->sas_topology_mutex);
  963. for (i = 0; i < port_info->num_phys; i++) {
  964. mptsas_sas_phy_pg0(ioc, &port_info->phy_info[i],
  965. (MPI_SAS_PHY_PGAD_FORM_PHY_NUMBER <<
  966. MPI_SAS_PHY_PGAD_FORM_SHIFT), i);
  967. mptsas_sas_device_pg0(ioc, &port_info->phy_info[i].identify,
  968. (MPI_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE <<
  969. MPI_SAS_DEVICE_PGAD_FORM_SHIFT), handle);
  970. port_info->phy_info[i].identify.phy_id =
  971. port_info->phy_info[i].phy_id;
  972. handle = port_info->phy_info[i].identify.handle;
  973. if (port_info->phy_info[i].attached.handle) {
  974. mptsas_sas_device_pg0(ioc,
  975. &port_info->phy_info[i].attached,
  976. (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
  977. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  978. port_info->phy_info[i].attached.handle);
  979. }
  980. mptsas_probe_one_phy(&ioc->sh->shost_gendev,
  981. &port_info->phy_info[i], *index, 1);
  982. (*index)++;
  983. }
  984. return 0;
  985. out_free_port_info:
  986. kfree(port_info);
  987. out:
  988. return error;
  989. }
  990. static int
  991. mptsas_probe_expander_phys(MPT_ADAPTER *ioc, u32 *handle, int *index)
  992. {
  993. struct mptsas_portinfo *port_info, *p;
  994. int error = -ENOMEM, i, j;
  995. port_info = kzalloc(sizeof(*port_info), GFP_KERNEL);
  996. if (!port_info)
  997. goto out;
  998. error = mptsas_sas_expander_pg0(ioc, port_info,
  999. (MPI_SAS_EXPAND_PGAD_FORM_GET_NEXT_HANDLE <<
  1000. MPI_SAS_EXPAND_PGAD_FORM_SHIFT), *handle);
  1001. if (error)
  1002. goto out_free_port_info;
  1003. *handle = port_info->handle;
  1004. mutex_lock(&ioc->sas_topology_mutex);
  1005. list_add_tail(&port_info->list, &ioc->sas_topology);
  1006. mutex_unlock(&ioc->sas_topology_mutex);
  1007. for (i = 0; i < port_info->num_phys; i++) {
  1008. struct device *parent;
  1009. mptsas_sas_expander_pg1(ioc, &port_info->phy_info[i],
  1010. (MPI_SAS_EXPAND_PGAD_FORM_HANDLE_PHY_NUM <<
  1011. MPI_SAS_EXPAND_PGAD_FORM_SHIFT), (i << 16) + *handle);
  1012. if (port_info->phy_info[i].identify.handle) {
  1013. mptsas_sas_device_pg0(ioc,
  1014. &port_info->phy_info[i].identify,
  1015. (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
  1016. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  1017. port_info->phy_info[i].identify.handle);
  1018. port_info->phy_info[i].identify.phy_id =
  1019. port_info->phy_info[i].phy_id;
  1020. }
  1021. if (port_info->phy_info[i].attached.handle) {
  1022. mptsas_sas_device_pg0(ioc,
  1023. &port_info->phy_info[i].attached,
  1024. (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
  1025. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  1026. port_info->phy_info[i].attached.handle);
  1027. }
  1028. /*
  1029. * If we find a parent port handle this expander is
  1030. * attached to another expander, else it hangs of the
  1031. * HBA phys.
  1032. */
  1033. parent = &ioc->sh->shost_gendev;
  1034. mutex_lock(&ioc->sas_topology_mutex);
  1035. list_for_each_entry(p, &ioc->sas_topology, list) {
  1036. for (j = 0; j < p->num_phys; j++) {
  1037. if (port_info->phy_info[i].identify.handle ==
  1038. p->phy_info[j].attached.handle)
  1039. parent = &p->phy_info[j].rphy->dev;
  1040. }
  1041. }
  1042. mutex_unlock(&ioc->sas_topology_mutex);
  1043. mptsas_probe_one_phy(parent, &port_info->phy_info[i],
  1044. *index, 0);
  1045. (*index)++;
  1046. }
  1047. return 0;
  1048. out_free_port_info:
  1049. kfree(port_info);
  1050. out:
  1051. return error;
  1052. }
  1053. static void
  1054. mptsas_scan_sas_topology(MPT_ADAPTER *ioc)
  1055. {
  1056. u32 handle = 0xFFFF;
  1057. int index = 0;
  1058. mptsas_probe_hba_phys(ioc, &index);
  1059. while (!mptsas_probe_expander_phys(ioc, &handle, &index))
  1060. ;
  1061. }
  1062. static struct mptsas_phyinfo *
  1063. mptsas_find_phyinfo_by_parent(MPT_ADAPTER *ioc, u16 parent_handle, u8 phy_id)
  1064. {
  1065. struct mptsas_portinfo *port_info;
  1066. struct mptsas_devinfo device_info;
  1067. struct mptsas_phyinfo *phy_info = NULL;
  1068. int i, error;
  1069. /*
  1070. * Retrieve the parent sas_address
  1071. */
  1072. error = mptsas_sas_device_pg0(ioc, &device_info,
  1073. (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
  1074. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  1075. parent_handle);
  1076. if (error) {
  1077. printk("mptsas: failed to retrieve device page\n");
  1078. return NULL;
  1079. }
  1080. /*
  1081. * The phy_info structures are never deallocated during lifetime of
  1082. * a host, so the code below is safe without additional refcounting.
  1083. */
  1084. mutex_lock(&ioc->sas_topology_mutex);
  1085. list_for_each_entry(port_info, &ioc->sas_topology, list) {
  1086. for (i = 0; i < port_info->num_phys; i++) {
  1087. if (port_info->phy_info[i].identify.sas_address ==
  1088. device_info.sas_address &&
  1089. port_info->phy_info[i].phy_id == phy_id) {
  1090. phy_info = &port_info->phy_info[i];
  1091. break;
  1092. }
  1093. }
  1094. }
  1095. mutex_unlock(&ioc->sas_topology_mutex);
  1096. return phy_info;
  1097. }
  1098. static struct mptsas_phyinfo *
  1099. mptsas_find_phyinfo_by_target(MPT_ADAPTER *ioc, u32 id)
  1100. {
  1101. struct mptsas_portinfo *port_info;
  1102. struct mptsas_phyinfo *phy_info = NULL;
  1103. int i;
  1104. /*
  1105. * The phy_info structures are never deallocated during lifetime of
  1106. * a host, so the code below is safe without additional refcounting.
  1107. */
  1108. mutex_lock(&ioc->sas_topology_mutex);
  1109. list_for_each_entry(port_info, &ioc->sas_topology, list) {
  1110. for (i = 0; i < port_info->num_phys; i++)
  1111. if (mptsas_is_end_device(&port_info->phy_info[i].attached))
  1112. if (port_info->phy_info[i].attached.id == id) {
  1113. phy_info = &port_info->phy_info[i];
  1114. break;
  1115. }
  1116. }
  1117. mutex_unlock(&ioc->sas_topology_mutex);
  1118. return phy_info;
  1119. }
  1120. static void
  1121. mptsas_hotplug_work(void *arg)
  1122. {
  1123. struct mptsas_hotplug_event *ev = arg;
  1124. MPT_ADAPTER *ioc = ev->ioc;
  1125. struct mptsas_phyinfo *phy_info;
  1126. struct sas_rphy *rphy;
  1127. struct scsi_device *sdev;
  1128. char *ds = NULL;
  1129. struct mptsas_devinfo sas_device;
  1130. switch (ev->event_type) {
  1131. case MPTSAS_DEL_DEVICE:
  1132. phy_info = mptsas_find_phyinfo_by_target(ioc, ev->id);
  1133. if (!phy_info) {
  1134. printk("mptsas: remove event for non-existant PHY.\n");
  1135. break;
  1136. }
  1137. if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_SSP_TARGET)
  1138. ds = "ssp";
  1139. if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_STP_TARGET)
  1140. ds = "stp";
  1141. if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_SATA_DEVICE)
  1142. ds = "sata";
  1143. printk(MYIOC_s_INFO_FMT
  1144. "removing %s device, channel %d, id %d, phy %d\n",
  1145. ioc->name, ds, ev->channel, ev->id, phy_info->phy_id);
  1146. if (phy_info->rphy) {
  1147. sas_rphy_delete(phy_info->rphy);
  1148. phy_info->rphy = NULL;
  1149. }
  1150. break;
  1151. case MPTSAS_ADD_DEVICE:
  1152. /*
  1153. * When there is no sas address,
  1154. * RAID volumes are being deleted,
  1155. * and hidden phy disk are being added.
  1156. * We don't know the SAS data yet,
  1157. * so lookup sas device page to get
  1158. * pertaining info
  1159. */
  1160. if (!ev->sas_address) {
  1161. if (mptsas_sas_device_pg0(ioc,
  1162. &sas_device, ev->id,
  1163. (MPI_SAS_DEVICE_PGAD_FORM_BUS_TARGET_ID <<
  1164. MPI_SAS_DEVICE_PGAD_FORM_SHIFT)))
  1165. break;
  1166. ev->handle = sas_device.handle;
  1167. ev->parent_handle = sas_device.handle_parent;
  1168. ev->channel = sas_device.channel;
  1169. ev->phy_id = sas_device.phy_id;
  1170. ev->sas_address = sas_device.sas_address;
  1171. ev->device_info = sas_device.device_info;
  1172. }
  1173. phy_info = mptsas_find_phyinfo_by_parent(ioc,
  1174. ev->parent_handle, ev->phy_id);
  1175. if (!phy_info) {
  1176. printk("mptsas: add event for non-existant PHY.\n");
  1177. break;
  1178. }
  1179. if (phy_info->rphy) {
  1180. printk("mptsas: trying to add existing device.\n");
  1181. break;
  1182. }
  1183. /* fill attached info */
  1184. phy_info->attached.handle = ev->handle;
  1185. phy_info->attached.phy_id = ev->phy_id;
  1186. phy_info->attached.port_id = phy_info->identify.port_id;
  1187. phy_info->attached.id = ev->id;
  1188. phy_info->attached.channel = ev->channel;
  1189. phy_info->attached.sas_address = ev->sas_address;
  1190. phy_info->attached.device_info = ev->device_info;
  1191. if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_SSP_TARGET)
  1192. ds = "ssp";
  1193. if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_STP_TARGET)
  1194. ds = "stp";
  1195. if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_SATA_DEVICE)
  1196. ds = "sata";
  1197. printk(MYIOC_s_INFO_FMT
  1198. "attaching %s device, channel %d, id %d, phy %d\n",
  1199. ioc->name, ds, ev->channel, ev->id, ev->phy_id);
  1200. rphy = sas_rphy_alloc(phy_info->phy);
  1201. if (!rphy)
  1202. break; /* non-fatal: an rphy can be added later */
  1203. rphy->scsi_target_id = phy_info->attached.id;
  1204. mptsas_parse_device_info(&rphy->identify, &phy_info->attached);
  1205. if (sas_rphy_add(rphy)) {
  1206. sas_rphy_free(rphy);
  1207. break;
  1208. }
  1209. phy_info->rphy = rphy;
  1210. break;
  1211. case MPTSAS_ADD_RAID:
  1212. sdev = scsi_device_lookup(
  1213. ioc->sh,
  1214. ioc->num_ports,
  1215. ev->id,
  1216. 0);
  1217. if (sdev) {
  1218. scsi_device_put(sdev);
  1219. break;
  1220. }
  1221. printk(MYIOC_s_INFO_FMT
  1222. "attaching device, channel %d, id %d\n",
  1223. ioc->name, ioc->num_ports, ev->id);
  1224. scsi_add_device(ioc->sh,
  1225. ioc->num_ports,
  1226. ev->id,
  1227. 0);
  1228. mpt_findImVolumes(ioc);
  1229. break;
  1230. case MPTSAS_DEL_RAID:
  1231. sdev = scsi_device_lookup(
  1232. ioc->sh,
  1233. ioc->num_ports,
  1234. ev->id,
  1235. 0);
  1236. if (!sdev)
  1237. break;
  1238. printk(MYIOC_s_INFO_FMT
  1239. "removing device, channel %d, id %d\n",
  1240. ioc->name, ioc->num_ports, ev->id);
  1241. scsi_remove_device(sdev);
  1242. scsi_device_put(sdev);
  1243. mpt_findImVolumes(ioc);
  1244. break;
  1245. }
  1246. kfree(ev);
  1247. }
  1248. static void
  1249. mptscsih_send_sas_event(MPT_ADAPTER *ioc,
  1250. EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *sas_event_data)
  1251. {
  1252. struct mptsas_hotplug_event *ev;
  1253. u32 device_info = le32_to_cpu(sas_event_data->DeviceInfo);
  1254. __le64 sas_address;
  1255. if ((device_info &
  1256. (MPI_SAS_DEVICE_INFO_SSP_TARGET |
  1257. MPI_SAS_DEVICE_INFO_STP_TARGET |
  1258. MPI_SAS_DEVICE_INFO_SATA_DEVICE )) == 0)
  1259. return;
  1260. if ((sas_event_data->ReasonCode &
  1261. (MPI_EVENT_SAS_DEV_STAT_RC_ADDED |
  1262. MPI_EVENT_SAS_DEV_STAT_RC_NOT_RESPONDING)) == 0)
  1263. return;
  1264. ev = kmalloc(sizeof(*ev), GFP_ATOMIC);
  1265. if (!ev) {
  1266. printk(KERN_WARNING "mptsas: lost hotplug event\n");
  1267. return;
  1268. }
  1269. INIT_WORK(&ev->work, mptsas_hotplug_work, ev);
  1270. ev->ioc = ioc;
  1271. ev->handle = le16_to_cpu(sas_event_data->DevHandle);
  1272. ev->parent_handle = le16_to_cpu(sas_event_data->ParentDevHandle);
  1273. ev->channel = sas_event_data->Bus;
  1274. ev->id = sas_event_data->TargetID;
  1275. ev->phy_id = sas_event_data->PhyNum;
  1276. memcpy(&sas_address, &sas_event_data->SASAddress, sizeof(__le64));
  1277. ev->sas_address = le64_to_cpu(sas_address);
  1278. ev->device_info = device_info;
  1279. if (sas_event_data->ReasonCode & MPI_EVENT_SAS_DEV_STAT_RC_ADDED)
  1280. ev->event_type = MPTSAS_ADD_DEVICE;
  1281. else
  1282. ev->event_type = MPTSAS_DEL_DEVICE;
  1283. schedule_work(&ev->work);
  1284. }
  1285. static void
  1286. mptscsih_send_raid_event(MPT_ADAPTER *ioc,
  1287. EVENT_DATA_RAID *raid_event_data)
  1288. {
  1289. struct mptsas_hotplug_event *ev;
  1290. RAID_VOL0_STATUS * volumeStatus;
  1291. if (ioc->bus_type != SAS)
  1292. return;
  1293. ev = kmalloc(sizeof(*ev), GFP_ATOMIC);
  1294. if (!ev) {
  1295. printk(KERN_WARNING "mptsas: lost hotplug event\n");
  1296. return;
  1297. }
  1298. memset(ev,0,sizeof(struct mptsas_hotplug_event));
  1299. INIT_WORK(&ev->work, mptsas_hotplug_work, ev);
  1300. ev->ioc = ioc;
  1301. ev->id = raid_event_data->VolumeID;
  1302. switch (raid_event_data->ReasonCode) {
  1303. case MPI_EVENT_RAID_RC_PHYSDISK_DELETED:
  1304. ev->event_type = MPTSAS_ADD_DEVICE;
  1305. break;
  1306. case MPI_EVENT_RAID_RC_PHYSDISK_CREATED:
  1307. ev->event_type = MPTSAS_DEL_DEVICE;
  1308. break;
  1309. case MPI_EVENT_RAID_RC_VOLUME_DELETED:
  1310. ev->event_type = MPTSAS_DEL_RAID;
  1311. break;
  1312. case MPI_EVENT_RAID_RC_VOLUME_CREATED:
  1313. ev->event_type = MPTSAS_ADD_RAID;
  1314. break;
  1315. case MPI_EVENT_RAID_RC_VOLUME_STATUS_CHANGED:
  1316. volumeStatus = (RAID_VOL0_STATUS *) &
  1317. raid_event_data->SettingsStatus;
  1318. ev->event_type = (volumeStatus->State ==
  1319. MPI_RAIDVOL0_STATUS_STATE_FAILED) ?
  1320. MPTSAS_DEL_RAID : MPTSAS_ADD_RAID;
  1321. break;
  1322. default:
  1323. break;
  1324. }
  1325. schedule_work(&ev->work);
  1326. }
  1327. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1328. /* work queue thread to clear the persitency table */
  1329. static void
  1330. mptscsih_sas_persist_clear_table(void * arg)
  1331. {
  1332. MPT_ADAPTER *ioc = (MPT_ADAPTER *)arg;
  1333. mptbase_sas_persist_operation(ioc, MPI_SAS_OP_CLEAR_NOT_PRESENT);
  1334. }
  1335. static int
  1336. mptsas_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *reply)
  1337. {
  1338. int rc=1;
  1339. u8 event = le32_to_cpu(reply->Event) & 0xFF;
  1340. if (!ioc->sh)
  1341. goto out;
  1342. switch (event) {
  1343. case MPI_EVENT_SAS_DEVICE_STATUS_CHANGE:
  1344. mptscsih_send_sas_event(ioc,
  1345. (EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *)reply->Data);
  1346. break;
  1347. case MPI_EVENT_INTEGRATED_RAID:
  1348. mptscsih_send_raid_event(ioc,
  1349. (EVENT_DATA_RAID *)reply->Data);
  1350. break;
  1351. case MPI_EVENT_PERSISTENT_TABLE_FULL:
  1352. INIT_WORK(&ioc->mptscsih_persistTask,
  1353. mptscsih_sas_persist_clear_table,
  1354. (void *)ioc);
  1355. schedule_work(&ioc->mptscsih_persistTask);
  1356. break;
  1357. default:
  1358. rc = mptscsih_event_process(ioc, reply);
  1359. break;
  1360. }
  1361. out:
  1362. return rc;
  1363. }
  1364. static int
  1365. mptsas_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  1366. {
  1367. struct Scsi_Host *sh;
  1368. MPT_SCSI_HOST *hd;
  1369. MPT_ADAPTER *ioc;
  1370. unsigned long flags;
  1371. int ii;
  1372. int numSGE = 0;
  1373. int scale;
  1374. int ioc_cap;
  1375. int error=0;
  1376. int r;
  1377. r = mpt_attach(pdev,id);
  1378. if (r)
  1379. return r;
  1380. ioc = pci_get_drvdata(pdev);
  1381. ioc->DoneCtx = mptsasDoneCtx;
  1382. ioc->TaskCtx = mptsasTaskCtx;
  1383. ioc->InternalCtx = mptsasInternalCtx;
  1384. /* Added sanity check on readiness of the MPT adapter.
  1385. */
  1386. if (ioc->last_state != MPI_IOC_STATE_OPERATIONAL) {
  1387. printk(MYIOC_s_WARN_FMT
  1388. "Skipping because it's not operational!\n",
  1389. ioc->name);
  1390. error = -ENODEV;
  1391. goto out_mptsas_probe;
  1392. }
  1393. if (!ioc->active) {
  1394. printk(MYIOC_s_WARN_FMT "Skipping because it's disabled!\n",
  1395. ioc->name);
  1396. error = -ENODEV;
  1397. goto out_mptsas_probe;
  1398. }
  1399. /* Sanity check - ensure at least 1 port is INITIATOR capable
  1400. */
  1401. ioc_cap = 0;
  1402. for (ii = 0; ii < ioc->facts.NumberOfPorts; ii++) {
  1403. if (ioc->pfacts[ii].ProtocolFlags &
  1404. MPI_PORTFACTS_PROTOCOL_INITIATOR)
  1405. ioc_cap++;
  1406. }
  1407. if (!ioc_cap) {
  1408. printk(MYIOC_s_WARN_FMT
  1409. "Skipping ioc=%p because SCSI Initiator mode "
  1410. "is NOT enabled!\n", ioc->name, ioc);
  1411. return 0;
  1412. }
  1413. sh = scsi_host_alloc(&mptsas_driver_template, sizeof(MPT_SCSI_HOST));
  1414. if (!sh) {
  1415. printk(MYIOC_s_WARN_FMT
  1416. "Unable to register controller with SCSI subsystem\n",
  1417. ioc->name);
  1418. error = -1;
  1419. goto out_mptsas_probe;
  1420. }
  1421. spin_lock_irqsave(&ioc->FreeQlock, flags);
  1422. /* Attach the SCSI Host to the IOC structure
  1423. */
  1424. ioc->sh = sh;
  1425. sh->io_port = 0;
  1426. sh->n_io_port = 0;
  1427. sh->irq = 0;
  1428. /* set 16 byte cdb's */
  1429. sh->max_cmd_len = 16;
  1430. sh->max_id = ioc->pfacts->MaxDevices + 1;
  1431. sh->transportt = mptsas_transport_template;
  1432. sh->max_lun = MPT_LAST_LUN + 1;
  1433. sh->max_channel = 0;
  1434. sh->this_id = ioc->pfacts[0].PortSCSIID;
  1435. /* Required entry.
  1436. */
  1437. sh->unique_id = ioc->id;
  1438. INIT_LIST_HEAD(&ioc->sas_topology);
  1439. mutex_init(&ioc->sas_topology_mutex);
  1440. mutex_init(&ioc->sas_mgmt.mutex);
  1441. init_completion(&ioc->sas_mgmt.done);
  1442. /* Verify that we won't exceed the maximum
  1443. * number of chain buffers
  1444. * We can optimize: ZZ = req_sz/sizeof(SGE)
  1445. * For 32bit SGE's:
  1446. * numSGE = 1 + (ZZ-1)*(maxChain -1) + ZZ
  1447. * + (req_sz - 64)/sizeof(SGE)
  1448. * A slightly different algorithm is required for
  1449. * 64bit SGEs.
  1450. */
  1451. scale = ioc->req_sz/(sizeof(dma_addr_t) + sizeof(u32));
  1452. if (sizeof(dma_addr_t) == sizeof(u64)) {
  1453. numSGE = (scale - 1) *
  1454. (ioc->facts.MaxChainDepth-1) + scale +
  1455. (ioc->req_sz - 60) / (sizeof(dma_addr_t) +
  1456. sizeof(u32));
  1457. } else {
  1458. numSGE = 1 + (scale - 1) *
  1459. (ioc->facts.MaxChainDepth-1) + scale +
  1460. (ioc->req_sz - 64) / (sizeof(dma_addr_t) +
  1461. sizeof(u32));
  1462. }
  1463. if (numSGE < sh->sg_tablesize) {
  1464. /* Reset this value */
  1465. dprintk((MYIOC_s_INFO_FMT
  1466. "Resetting sg_tablesize to %d from %d\n",
  1467. ioc->name, numSGE, sh->sg_tablesize));
  1468. sh->sg_tablesize = numSGE;
  1469. }
  1470. spin_unlock_irqrestore(&ioc->FreeQlock, flags);
  1471. hd = (MPT_SCSI_HOST *) sh->hostdata;
  1472. hd->ioc = ioc;
  1473. /* SCSI needs scsi_cmnd lookup table!
  1474. * (with size equal to req_depth*PtrSz!)
  1475. */
  1476. hd->ScsiLookup = kcalloc(ioc->req_depth, sizeof(void *), GFP_ATOMIC);
  1477. if (!hd->ScsiLookup) {
  1478. error = -ENOMEM;
  1479. goto out_mptsas_probe;
  1480. }
  1481. dprintk((MYIOC_s_INFO_FMT "ScsiLookup @ %p\n",
  1482. ioc->name, hd->ScsiLookup));
  1483. /* Allocate memory for the device structures.
  1484. * A non-Null pointer at an offset
  1485. * indicates a device exists.
  1486. * max_id = 1 + maximum id (hosts.h)
  1487. */
  1488. hd->Targets = kcalloc(sh->max_id, sizeof(void *), GFP_ATOMIC);
  1489. if (!hd->Targets) {
  1490. error = -ENOMEM;
  1491. goto out_mptsas_probe;
  1492. }
  1493. dprintk((KERN_INFO " vtarget @ %p\n", hd->Targets));
  1494. /* Clear the TM flags
  1495. */
  1496. hd->tmPending = 0;
  1497. hd->tmState = TM_STATE_NONE;
  1498. hd->resetPending = 0;
  1499. hd->abortSCpnt = NULL;
  1500. /* Clear the pointer used to store
  1501. * single-threaded commands, i.e., those
  1502. * issued during a bus scan, dv and
  1503. * configuration pages.
  1504. */
  1505. hd->cmdPtr = NULL;
  1506. /* Initialize this SCSI Hosts' timers
  1507. * To use, set the timer expires field
  1508. * and add_timer
  1509. */
  1510. init_timer(&hd->timer);
  1511. hd->timer.data = (unsigned long) hd;
  1512. hd->timer.function = mptscsih_timer_expired;
  1513. hd->mpt_pq_filter = mpt_pq_filter;
  1514. ioc->sas_data.ptClear = mpt_pt_clear;
  1515. if (ioc->sas_data.ptClear==1) {
  1516. mptbase_sas_persist_operation(
  1517. ioc, MPI_SAS_OP_CLEAR_ALL_PERSISTENT);
  1518. }
  1519. ddvprintk((MYIOC_s_INFO_FMT
  1520. "mpt_pq_filter %x mpt_pq_filter %x\n",
  1521. ioc->name,
  1522. mpt_pq_filter,
  1523. mpt_pq_filter));
  1524. init_waitqueue_head(&hd->scandv_waitq);
  1525. hd->scandv_wait_done = 0;
  1526. hd->last_queue_full = 0;
  1527. error = scsi_add_host(sh, &ioc->pcidev->dev);
  1528. if (error) {
  1529. dprintk((KERN_ERR MYNAM
  1530. "scsi_add_host failed\n"));
  1531. goto out_mptsas_probe;
  1532. }
  1533. mptsas_scan_sas_topology(ioc);
  1534. /*
  1535. Reporting RAID volumes.
  1536. */
  1537. if (!ioc->raid_data.pIocPg2)
  1538. return 0;
  1539. if (!ioc->raid_data.pIocPg2->NumActiveVolumes)
  1540. return 0;
  1541. for (ii=0;ii<ioc->raid_data.pIocPg2->NumActiveVolumes;ii++) {
  1542. scsi_add_device(sh,
  1543. ioc->num_ports,
  1544. ioc->raid_data.pIocPg2->RaidVolume[ii].VolumeID,
  1545. 0);
  1546. }
  1547. return 0;
  1548. out_mptsas_probe:
  1549. mptscsih_remove(pdev);
  1550. return error;
  1551. }
  1552. static void __devexit mptsas_remove(struct pci_dev *pdev)
  1553. {
  1554. MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
  1555. struct mptsas_portinfo *p, *n;
  1556. sas_remove_host(ioc->sh);
  1557. mutex_lock(&ioc->sas_topology_mutex);
  1558. list_for_each_entry_safe(p, n, &ioc->sas_topology, list) {
  1559. list_del(&p->list);
  1560. kfree(p);
  1561. }
  1562. mutex_unlock(&ioc->sas_topology_mutex);
  1563. mptscsih_remove(pdev);
  1564. }
  1565. static struct pci_device_id mptsas_pci_table[] = {
  1566. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064,
  1567. PCI_ANY_ID, PCI_ANY_ID },
  1568. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1066,
  1569. PCI_ANY_ID, PCI_ANY_ID },
  1570. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1068,
  1571. PCI_ANY_ID, PCI_ANY_ID },
  1572. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064E,
  1573. PCI_ANY_ID, PCI_ANY_ID },
  1574. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1066E,
  1575. PCI_ANY_ID, PCI_ANY_ID },
  1576. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1068E,
  1577. PCI_ANY_ID, PCI_ANY_ID },
  1578. {0} /* Terminating entry */
  1579. };
  1580. MODULE_DEVICE_TABLE(pci, mptsas_pci_table);
  1581. static struct pci_driver mptsas_driver = {
  1582. .name = "mptsas",
  1583. .id_table = mptsas_pci_table,
  1584. .probe = mptsas_probe,
  1585. .remove = __devexit_p(mptsas_remove),
  1586. .shutdown = mptscsih_shutdown,
  1587. #ifdef CONFIG_PM
  1588. .suspend = mptscsih_suspend,
  1589. .resume = mptscsih_resume,
  1590. #endif
  1591. };
  1592. static int __init
  1593. mptsas_init(void)
  1594. {
  1595. show_mptmod_ver(my_NAME, my_VERSION);
  1596. mptsas_transport_template =
  1597. sas_attach_transport(&mptsas_transport_functions);
  1598. if (!mptsas_transport_template)
  1599. return -ENODEV;
  1600. mptsasDoneCtx = mpt_register(mptscsih_io_done, MPTSAS_DRIVER);
  1601. mptsasTaskCtx = mpt_register(mptscsih_taskmgmt_complete, MPTSAS_DRIVER);
  1602. mptsasInternalCtx =
  1603. mpt_register(mptscsih_scandv_complete, MPTSAS_DRIVER);
  1604. mptsasMgmtCtx = mpt_register(mptsas_mgmt_done, MPTSAS_DRIVER);
  1605. if (mpt_event_register(mptsasDoneCtx, mptsas_event_process) == 0) {
  1606. devtprintk((KERN_INFO MYNAM
  1607. ": Registered for IOC event notifications\n"));
  1608. }
  1609. if (mpt_reset_register(mptsasDoneCtx, mptscsih_ioc_reset) == 0) {
  1610. dprintk((KERN_INFO MYNAM
  1611. ": Registered for IOC reset notifications\n"));
  1612. }
  1613. return pci_register_driver(&mptsas_driver);
  1614. }
  1615. static void __exit
  1616. mptsas_exit(void)
  1617. {
  1618. pci_unregister_driver(&mptsas_driver);
  1619. sas_release_transport(mptsas_transport_template);
  1620. mpt_reset_deregister(mptsasDoneCtx);
  1621. mpt_event_deregister(mptsasDoneCtx);
  1622. mpt_deregister(mptsasMgmtCtx);
  1623. mpt_deregister(mptsasInternalCtx);
  1624. mpt_deregister(mptsasTaskCtx);
  1625. mpt_deregister(mptsasDoneCtx);
  1626. }
  1627. module_init(mptsas_init);
  1628. module_exit(mptsas_exit);