mptsas.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413
  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. MPTSAS_IGNORE_EVENT,
  80. };
  81. struct mptsas_hotplug_event {
  82. struct work_struct work;
  83. MPT_ADAPTER *ioc;
  84. enum mptsas_hotplug_action event_type;
  85. u64 sas_address;
  86. u32 channel;
  87. u32 id;
  88. u32 device_info;
  89. u16 handle;
  90. u16 parent_handle;
  91. u8 phy_id;
  92. u8 phys_disk_num;
  93. u8 phys_disk_num_valid;
  94. };
  95. struct mptsas_discovery_event {
  96. struct work_struct work;
  97. MPT_ADAPTER *ioc;
  98. };
  99. /*
  100. * SAS topology structures
  101. *
  102. * The MPT Fusion firmware interface spreads information about the
  103. * SAS topology over many manufacture pages, thus we need some data
  104. * structure to collect it and process it for the SAS transport class.
  105. */
  106. struct mptsas_devinfo {
  107. u16 handle; /* unique id to address this device */
  108. u16 handle_parent; /* unique id to address parent device */
  109. u16 handle_enclosure; /* enclosure identifier of the enclosure */
  110. u16 slot; /* physical slot in enclosure */
  111. u8 phy_id; /* phy number of parent device */
  112. u8 port_id; /* sas physical port this device
  113. is assoc'd with */
  114. u8 id; /* logical target id of this device */
  115. u8 channel; /* logical bus number of this device */
  116. u64 sas_address; /* WWN of this device,
  117. SATA is assigned by HBA,expander */
  118. u32 device_info; /* bitfield detailed info about this device */
  119. };
  120. struct mptsas_phyinfo {
  121. u8 phy_id; /* phy index */
  122. u8 port_id; /* port number this phy is part of */
  123. u8 negotiated_link_rate; /* nego'd link rate for this phy */
  124. u8 hw_link_rate; /* hardware max/min phys link rate */
  125. u8 programmed_link_rate; /* programmed max/min phy link rate */
  126. struct mptsas_devinfo identify; /* point to phy device info */
  127. struct mptsas_devinfo attached; /* point to attached device info */
  128. struct sas_phy *phy;
  129. struct sas_rphy *rphy;
  130. struct scsi_target *starget;
  131. };
  132. struct mptsas_portinfo {
  133. struct list_head list;
  134. u16 handle; /* unique id to address this */
  135. u8 num_phys; /* number of phys */
  136. struct mptsas_phyinfo *phy_info;
  137. };
  138. struct mptsas_enclosure {
  139. u64 enclosure_logical_id; /* The WWN for the enclosure */
  140. u16 enclosure_handle; /* unique id to address this */
  141. u16 flags; /* details enclosure management */
  142. u16 num_slot; /* num slots */
  143. u16 start_slot; /* first slot */
  144. u8 start_id; /* starting logical target id */
  145. u8 start_channel; /* starting logical channel id */
  146. u8 sep_id; /* SEP device logical target id */
  147. u8 sep_channel; /* SEP channel logical channel id */
  148. };
  149. #ifdef SASDEBUG
  150. static void mptsas_print_phy_data(MPI_SAS_IO_UNIT0_PHY_DATA *phy_data)
  151. {
  152. printk("---- IO UNIT PAGE 0 ------------\n");
  153. printk("Handle=0x%X\n",
  154. le16_to_cpu(phy_data->AttachedDeviceHandle));
  155. printk("Controller Handle=0x%X\n",
  156. le16_to_cpu(phy_data->ControllerDevHandle));
  157. printk("Port=0x%X\n", phy_data->Port);
  158. printk("Port Flags=0x%X\n", phy_data->PortFlags);
  159. printk("PHY Flags=0x%X\n", phy_data->PhyFlags);
  160. printk("Negotiated Link Rate=0x%X\n", phy_data->NegotiatedLinkRate);
  161. printk("Controller PHY Device Info=0x%X\n",
  162. le32_to_cpu(phy_data->ControllerPhyDeviceInfo));
  163. printk("DiscoveryStatus=0x%X\n",
  164. le32_to_cpu(phy_data->DiscoveryStatus));
  165. printk("\n");
  166. }
  167. static void mptsas_print_phy_pg0(SasPhyPage0_t *pg0)
  168. {
  169. __le64 sas_address;
  170. memcpy(&sas_address, &pg0->SASAddress, sizeof(__le64));
  171. printk("---- SAS PHY PAGE 0 ------------\n");
  172. printk("Attached Device Handle=0x%X\n",
  173. le16_to_cpu(pg0->AttachedDevHandle));
  174. printk("SAS Address=0x%llX\n",
  175. (unsigned long long)le64_to_cpu(sas_address));
  176. printk("Attached PHY Identifier=0x%X\n", pg0->AttachedPhyIdentifier);
  177. printk("Attached Device Info=0x%X\n",
  178. le32_to_cpu(pg0->AttachedDeviceInfo));
  179. printk("Programmed Link Rate=0x%X\n", pg0->ProgrammedLinkRate);
  180. printk("Change Count=0x%X\n", pg0->ChangeCount);
  181. printk("PHY Info=0x%X\n", le32_to_cpu(pg0->PhyInfo));
  182. printk("\n");
  183. }
  184. static void mptsas_print_phy_pg1(SasPhyPage1_t *pg1)
  185. {
  186. printk("---- SAS PHY PAGE 1 ------------\n");
  187. printk("Invalid Dword Count=0x%x\n", pg1->InvalidDwordCount);
  188. printk("Running Disparity Error Count=0x%x\n",
  189. pg1->RunningDisparityErrorCount);
  190. printk("Loss Dword Synch Count=0x%x\n", pg1->LossDwordSynchCount);
  191. printk("PHY Reset Problem Count=0x%x\n", pg1->PhyResetProblemCount);
  192. printk("\n");
  193. }
  194. static void mptsas_print_device_pg0(SasDevicePage0_t *pg0)
  195. {
  196. __le64 sas_address;
  197. memcpy(&sas_address, &pg0->SASAddress, sizeof(__le64));
  198. printk("---- SAS DEVICE PAGE 0 ---------\n");
  199. printk("Handle=0x%X\n" ,le16_to_cpu(pg0->DevHandle));
  200. printk("Parent Handle=0x%X\n" ,le16_to_cpu(pg0->ParentDevHandle));
  201. printk("Enclosure Handle=0x%X\n", le16_to_cpu(pg0->EnclosureHandle));
  202. printk("Slot=0x%X\n", le16_to_cpu(pg0->Slot));
  203. printk("SAS Address=0x%llX\n", le64_to_cpu(sas_address));
  204. printk("Target ID=0x%X\n", pg0->TargetID);
  205. printk("Bus=0x%X\n", pg0->Bus);
  206. /* The PhyNum field specifies the PHY number of the parent
  207. * device this device is linked to
  208. */
  209. printk("Parent Phy Num=0x%X\n", pg0->PhyNum);
  210. printk("Access Status=0x%X\n", le16_to_cpu(pg0->AccessStatus));
  211. printk("Device Info=0x%X\n", le32_to_cpu(pg0->DeviceInfo));
  212. printk("Flags=0x%X\n", le16_to_cpu(pg0->Flags));
  213. printk("Physical Port=0x%X\n", pg0->PhysicalPort);
  214. printk("\n");
  215. }
  216. static void mptsas_print_expander_pg1(SasExpanderPage1_t *pg1)
  217. {
  218. printk("---- SAS EXPANDER PAGE 1 ------------\n");
  219. printk("Physical Port=0x%X\n", pg1->PhysicalPort);
  220. printk("PHY Identifier=0x%X\n", pg1->PhyIdentifier);
  221. printk("Negotiated Link Rate=0x%X\n", pg1->NegotiatedLinkRate);
  222. printk("Programmed Link Rate=0x%X\n", pg1->ProgrammedLinkRate);
  223. printk("Hardware Link Rate=0x%X\n", pg1->HwLinkRate);
  224. printk("Owner Device Handle=0x%X\n",
  225. le16_to_cpu(pg1->OwnerDevHandle));
  226. printk("Attached Device Handle=0x%X\n",
  227. le16_to_cpu(pg1->AttachedDevHandle));
  228. }
  229. #else
  230. #define mptsas_print_phy_data(phy_data) do { } while (0)
  231. #define mptsas_print_phy_pg0(pg0) do { } while (0)
  232. #define mptsas_print_phy_pg1(pg1) do { } while (0)
  233. #define mptsas_print_device_pg0(pg0) do { } while (0)
  234. #define mptsas_print_expander_pg1(pg1) do { } while (0)
  235. #endif
  236. static inline MPT_ADAPTER *phy_to_ioc(struct sas_phy *phy)
  237. {
  238. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  239. return ((MPT_SCSI_HOST *)shost->hostdata)->ioc;
  240. }
  241. static inline MPT_ADAPTER *rphy_to_ioc(struct sas_rphy *rphy)
  242. {
  243. struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
  244. return ((MPT_SCSI_HOST *)shost->hostdata)->ioc;
  245. }
  246. /*
  247. * mptsas_find_portinfo_by_handle
  248. *
  249. * This function should be called with the sas_topology_mutex already held
  250. */
  251. static struct mptsas_portinfo *
  252. mptsas_find_portinfo_by_handle(MPT_ADAPTER *ioc, u16 handle)
  253. {
  254. struct mptsas_portinfo *port_info, *rc=NULL;
  255. int i;
  256. list_for_each_entry(port_info, &ioc->sas_topology, list)
  257. for (i = 0; i < port_info->num_phys; i++)
  258. if (port_info->phy_info[i].identify.handle == handle) {
  259. rc = port_info;
  260. goto out;
  261. }
  262. out:
  263. return rc;
  264. }
  265. /*
  266. * Returns true if there is a scsi end device
  267. */
  268. static inline int
  269. mptsas_is_end_device(struct mptsas_devinfo * attached)
  270. {
  271. if ((attached->handle) &&
  272. (attached->device_info &
  273. MPI_SAS_DEVICE_INFO_END_DEVICE) &&
  274. ((attached->device_info &
  275. MPI_SAS_DEVICE_INFO_SSP_TARGET) |
  276. (attached->device_info &
  277. MPI_SAS_DEVICE_INFO_STP_TARGET) |
  278. (attached->device_info &
  279. MPI_SAS_DEVICE_INFO_SATA_DEVICE)))
  280. return 1;
  281. else
  282. return 0;
  283. }
  284. static int
  285. mptsas_sas_enclosure_pg0(MPT_ADAPTER *ioc, struct mptsas_enclosure *enclosure,
  286. u32 form, u32 form_specific)
  287. {
  288. ConfigExtendedPageHeader_t hdr;
  289. CONFIGPARMS cfg;
  290. SasEnclosurePage0_t *buffer;
  291. dma_addr_t dma_handle;
  292. int error;
  293. __le64 le_identifier;
  294. memset(&hdr, 0, sizeof(hdr));
  295. hdr.PageVersion = MPI_SASENCLOSURE0_PAGEVERSION;
  296. hdr.PageNumber = 0;
  297. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  298. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_ENCLOSURE;
  299. cfg.cfghdr.ehdr = &hdr;
  300. cfg.physAddr = -1;
  301. cfg.pageAddr = form + form_specific;
  302. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  303. cfg.dir = 0; /* read */
  304. cfg.timeout = 10;
  305. error = mpt_config(ioc, &cfg);
  306. if (error)
  307. goto out;
  308. if (!hdr.ExtPageLength) {
  309. error = -ENXIO;
  310. goto out;
  311. }
  312. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  313. &dma_handle);
  314. if (!buffer) {
  315. error = -ENOMEM;
  316. goto out;
  317. }
  318. cfg.physAddr = dma_handle;
  319. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  320. error = mpt_config(ioc, &cfg);
  321. if (error)
  322. goto out_free_consistent;
  323. /* save config data */
  324. memcpy(&le_identifier, &buffer->EnclosureLogicalID, sizeof(__le64));
  325. enclosure->enclosure_logical_id = le64_to_cpu(le_identifier);
  326. enclosure->enclosure_handle = le16_to_cpu(buffer->EnclosureHandle);
  327. enclosure->flags = le16_to_cpu(buffer->Flags);
  328. enclosure->num_slot = le16_to_cpu(buffer->NumSlots);
  329. enclosure->start_slot = le16_to_cpu(buffer->StartSlot);
  330. enclosure->start_id = buffer->StartTargetID;
  331. enclosure->start_channel = buffer->StartBus;
  332. enclosure->sep_id = buffer->SEPTargetID;
  333. enclosure->sep_channel = buffer->SEPBus;
  334. out_free_consistent:
  335. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  336. buffer, dma_handle);
  337. out:
  338. return error;
  339. }
  340. static int
  341. mptsas_slave_configure(struct scsi_device *sdev)
  342. {
  343. struct Scsi_Host *host = sdev->host;
  344. MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)host->hostdata;
  345. /*
  346. * RAID volumes placed beyond the last expected port.
  347. * Ignore sending sas mode pages in that case..
  348. */
  349. if (sdev->channel < hd->ioc->num_ports)
  350. sas_read_port_mode_page(sdev);
  351. return mptscsih_slave_configure(sdev);
  352. }
  353. /*
  354. * This is pretty ugly. We will be able to seriously clean it up
  355. * once the DV code in mptscsih goes away and we can properly
  356. * implement ->target_alloc.
  357. */
  358. static int
  359. mptsas_slave_alloc(struct scsi_device *sdev)
  360. {
  361. struct Scsi_Host *host = sdev->host;
  362. MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)host->hostdata;
  363. struct sas_rphy *rphy;
  364. struct mptsas_portinfo *p;
  365. VirtTarget *vtarget;
  366. VirtDevice *vdev;
  367. struct scsi_target *starget;
  368. u32 target_id;
  369. int i;
  370. vdev = kzalloc(sizeof(VirtDevice), GFP_KERNEL);
  371. if (!vdev) {
  372. printk(MYIOC_s_ERR_FMT "slave_alloc kmalloc(%zd) FAILED!\n",
  373. hd->ioc->name, sizeof(VirtDevice));
  374. return -ENOMEM;
  375. }
  376. sdev->hostdata = vdev;
  377. starget = scsi_target(sdev);
  378. vtarget = starget->hostdata;
  379. vtarget->ioc_id = hd->ioc->id;
  380. vdev->vtarget = vtarget;
  381. if (vtarget->num_luns == 0) {
  382. vtarget->tflags = MPT_TARGET_FLAGS_Q_YES|MPT_TARGET_FLAGS_VALID_INQUIRY;
  383. hd->Targets[sdev->id] = vtarget;
  384. }
  385. /*
  386. RAID volumes placed beyond the last expected port.
  387. */
  388. if (sdev->channel == hd->ioc->num_ports) {
  389. target_id = sdev->id;
  390. vtarget->bus_id = 0;
  391. vdev->lun = 0;
  392. goto out;
  393. }
  394. rphy = dev_to_rphy(sdev->sdev_target->dev.parent);
  395. mutex_lock(&hd->ioc->sas_topology_mutex);
  396. list_for_each_entry(p, &hd->ioc->sas_topology, list) {
  397. for (i = 0; i < p->num_phys; i++) {
  398. if (p->phy_info[i].attached.sas_address ==
  399. rphy->identify.sas_address) {
  400. target_id = p->phy_info[i].attached.id;
  401. vtarget->bus_id = p->phy_info[i].attached.channel;
  402. vdev->lun = sdev->lun;
  403. p->phy_info[i].starget = sdev->sdev_target;
  404. /*
  405. * Exposing hidden disk (RAID)
  406. */
  407. if (mptscsih_is_phys_disk(hd->ioc, target_id)) {
  408. target_id = mptscsih_raid_id_to_num(hd,
  409. target_id);
  410. vdev->vtarget->tflags |=
  411. MPT_TARGET_FLAGS_RAID_COMPONENT;
  412. sdev->no_uld_attach = 1;
  413. }
  414. mutex_unlock(&hd->ioc->sas_topology_mutex);
  415. goto out;
  416. }
  417. }
  418. }
  419. mutex_unlock(&hd->ioc->sas_topology_mutex);
  420. kfree(vdev);
  421. return -ENXIO;
  422. out:
  423. vtarget->target_id = target_id;
  424. vtarget->num_luns++;
  425. return 0;
  426. }
  427. static void
  428. mptsas_slave_destroy(struct scsi_device *sdev)
  429. {
  430. struct Scsi_Host *host = sdev->host;
  431. MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)host->hostdata;
  432. VirtDevice *vdev;
  433. /*
  434. * Issue target reset to flush firmware outstanding commands.
  435. */
  436. vdev = sdev->hostdata;
  437. if (vdev->configured_lun){
  438. if (mptscsih_TMHandler(hd,
  439. MPI_SCSITASKMGMT_TASKTYPE_TARGET_RESET,
  440. vdev->vtarget->bus_id,
  441. vdev->vtarget->target_id,
  442. 0, 0, 5 /* 5 second timeout */)
  443. < 0){
  444. /* The TM request failed!
  445. * Fatal error case.
  446. */
  447. printk(MYIOC_s_WARN_FMT
  448. "Error processing TaskMgmt id=%d TARGET_RESET\n",
  449. hd->ioc->name,
  450. vdev->vtarget->target_id);
  451. hd->tmPending = 0;
  452. hd->tmState = TM_STATE_NONE;
  453. }
  454. }
  455. mptscsih_slave_destroy(sdev);
  456. }
  457. static struct scsi_host_template mptsas_driver_template = {
  458. .module = THIS_MODULE,
  459. .proc_name = "mptsas",
  460. .proc_info = mptscsih_proc_info,
  461. .name = "MPT SPI Host",
  462. .info = mptscsih_info,
  463. .queuecommand = mptscsih_qcmd,
  464. .target_alloc = mptscsih_target_alloc,
  465. .slave_alloc = mptsas_slave_alloc,
  466. .slave_configure = mptsas_slave_configure,
  467. .target_destroy = mptscsih_target_destroy,
  468. .slave_destroy = mptsas_slave_destroy,
  469. .change_queue_depth = mptscsih_change_queue_depth,
  470. .eh_abort_handler = mptscsih_abort,
  471. .eh_device_reset_handler = mptscsih_dev_reset,
  472. .eh_bus_reset_handler = mptscsih_bus_reset,
  473. .eh_host_reset_handler = mptscsih_host_reset,
  474. .bios_param = mptscsih_bios_param,
  475. .can_queue = MPT_FC_CAN_QUEUE,
  476. .this_id = -1,
  477. .sg_tablesize = MPT_SCSI_SG_DEPTH,
  478. .max_sectors = 8192,
  479. .cmd_per_lun = 7,
  480. .use_clustering = ENABLE_CLUSTERING,
  481. };
  482. static int mptsas_get_linkerrors(struct sas_phy *phy)
  483. {
  484. MPT_ADAPTER *ioc = phy_to_ioc(phy);
  485. ConfigExtendedPageHeader_t hdr;
  486. CONFIGPARMS cfg;
  487. SasPhyPage1_t *buffer;
  488. dma_addr_t dma_handle;
  489. int error;
  490. hdr.PageVersion = MPI_SASPHY1_PAGEVERSION;
  491. hdr.ExtPageLength = 0;
  492. hdr.PageNumber = 1 /* page number 1*/;
  493. hdr.Reserved1 = 0;
  494. hdr.Reserved2 = 0;
  495. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  496. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_PHY;
  497. cfg.cfghdr.ehdr = &hdr;
  498. cfg.physAddr = -1;
  499. cfg.pageAddr = phy->identify.phy_identifier;
  500. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  501. cfg.dir = 0; /* read */
  502. cfg.timeout = 10;
  503. error = mpt_config(ioc, &cfg);
  504. if (error)
  505. return error;
  506. if (!hdr.ExtPageLength)
  507. return -ENXIO;
  508. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  509. &dma_handle);
  510. if (!buffer)
  511. return -ENOMEM;
  512. cfg.physAddr = dma_handle;
  513. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  514. error = mpt_config(ioc, &cfg);
  515. if (error)
  516. goto out_free_consistent;
  517. mptsas_print_phy_pg1(buffer);
  518. phy->invalid_dword_count = le32_to_cpu(buffer->InvalidDwordCount);
  519. phy->running_disparity_error_count =
  520. le32_to_cpu(buffer->RunningDisparityErrorCount);
  521. phy->loss_of_dword_sync_count =
  522. le32_to_cpu(buffer->LossDwordSynchCount);
  523. phy->phy_reset_problem_count =
  524. le32_to_cpu(buffer->PhyResetProblemCount);
  525. out_free_consistent:
  526. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  527. buffer, dma_handle);
  528. return error;
  529. }
  530. static int mptsas_mgmt_done(MPT_ADAPTER *ioc, MPT_FRAME_HDR *req,
  531. MPT_FRAME_HDR *reply)
  532. {
  533. ioc->sas_mgmt.status |= MPT_SAS_MGMT_STATUS_COMMAND_GOOD;
  534. if (reply != NULL) {
  535. ioc->sas_mgmt.status |= MPT_SAS_MGMT_STATUS_RF_VALID;
  536. memcpy(ioc->sas_mgmt.reply, reply,
  537. min(ioc->reply_sz, 4 * reply->u.reply.MsgLength));
  538. }
  539. complete(&ioc->sas_mgmt.done);
  540. return 1;
  541. }
  542. static int mptsas_phy_reset(struct sas_phy *phy, int hard_reset)
  543. {
  544. MPT_ADAPTER *ioc = phy_to_ioc(phy);
  545. SasIoUnitControlRequest_t *req;
  546. SasIoUnitControlReply_t *reply;
  547. MPT_FRAME_HDR *mf;
  548. MPIHeader_t *hdr;
  549. unsigned long timeleft;
  550. int error = -ERESTARTSYS;
  551. /* not implemented for expanders */
  552. if (phy->identify.target_port_protocols & SAS_PROTOCOL_SMP)
  553. return -ENXIO;
  554. if (mutex_lock_interruptible(&ioc->sas_mgmt.mutex))
  555. goto out;
  556. mf = mpt_get_msg_frame(mptsasMgmtCtx, ioc);
  557. if (!mf) {
  558. error = -ENOMEM;
  559. goto out_unlock;
  560. }
  561. hdr = (MPIHeader_t *) mf;
  562. req = (SasIoUnitControlRequest_t *)mf;
  563. memset(req, 0, sizeof(SasIoUnitControlRequest_t));
  564. req->Function = MPI_FUNCTION_SAS_IO_UNIT_CONTROL;
  565. req->MsgContext = hdr->MsgContext;
  566. req->Operation = hard_reset ?
  567. MPI_SAS_OP_PHY_HARD_RESET : MPI_SAS_OP_PHY_LINK_RESET;
  568. req->PhyNum = phy->identify.phy_identifier;
  569. mpt_put_msg_frame(mptsasMgmtCtx, ioc, mf);
  570. timeleft = wait_for_completion_timeout(&ioc->sas_mgmt.done,
  571. 10 * HZ);
  572. if (!timeleft) {
  573. /* On timeout reset the board */
  574. mpt_free_msg_frame(ioc, mf);
  575. mpt_HardResetHandler(ioc, CAN_SLEEP);
  576. error = -ETIMEDOUT;
  577. goto out_unlock;
  578. }
  579. /* a reply frame is expected */
  580. if ((ioc->sas_mgmt.status &
  581. MPT_IOCTL_STATUS_RF_VALID) == 0) {
  582. error = -ENXIO;
  583. goto out_unlock;
  584. }
  585. /* process the completed Reply Message Frame */
  586. reply = (SasIoUnitControlReply_t *)ioc->sas_mgmt.reply;
  587. if (reply->IOCStatus != MPI_IOCSTATUS_SUCCESS) {
  588. printk("%s: IOCStatus=0x%X IOCLogInfo=0x%X\n",
  589. __FUNCTION__,
  590. reply->IOCStatus,
  591. reply->IOCLogInfo);
  592. error = -ENXIO;
  593. goto out_unlock;
  594. }
  595. error = 0;
  596. out_unlock:
  597. mutex_unlock(&ioc->sas_mgmt.mutex);
  598. out:
  599. return error;
  600. }
  601. static int
  602. mptsas_get_enclosure_identifier(struct sas_rphy *rphy, u64 *identifier)
  603. {
  604. MPT_ADAPTER *ioc = rphy_to_ioc(rphy);
  605. int i, error;
  606. struct mptsas_portinfo *p;
  607. struct mptsas_enclosure enclosure_info;
  608. u64 enclosure_handle;
  609. mutex_lock(&ioc->sas_topology_mutex);
  610. list_for_each_entry(p, &ioc->sas_topology, list) {
  611. for (i = 0; i < p->num_phys; i++) {
  612. if (p->phy_info[i].attached.sas_address ==
  613. rphy->identify.sas_address) {
  614. enclosure_handle = p->phy_info[i].
  615. attached.handle_enclosure;
  616. goto found_info;
  617. }
  618. }
  619. }
  620. mutex_unlock(&ioc->sas_topology_mutex);
  621. return -ENXIO;
  622. found_info:
  623. mutex_unlock(&ioc->sas_topology_mutex);
  624. memset(&enclosure_info, 0, sizeof(struct mptsas_enclosure));
  625. error = mptsas_sas_enclosure_pg0(ioc, &enclosure_info,
  626. (MPI_SAS_ENCLOS_PGAD_FORM_HANDLE <<
  627. MPI_SAS_ENCLOS_PGAD_FORM_SHIFT), enclosure_handle);
  628. if (!error)
  629. *identifier = enclosure_info.enclosure_logical_id;
  630. return error;
  631. }
  632. static int
  633. mptsas_get_bay_identifier(struct sas_rphy *rphy)
  634. {
  635. MPT_ADAPTER *ioc = rphy_to_ioc(rphy);
  636. struct mptsas_portinfo *p;
  637. int i, rc;
  638. mutex_lock(&ioc->sas_topology_mutex);
  639. list_for_each_entry(p, &ioc->sas_topology, list) {
  640. for (i = 0; i < p->num_phys; i++) {
  641. if (p->phy_info[i].attached.sas_address ==
  642. rphy->identify.sas_address) {
  643. rc = p->phy_info[i].attached.slot;
  644. goto out;
  645. }
  646. }
  647. }
  648. rc = -ENXIO;
  649. out:
  650. mutex_unlock(&ioc->sas_topology_mutex);
  651. return rc;
  652. }
  653. static struct sas_function_template mptsas_transport_functions = {
  654. .get_linkerrors = mptsas_get_linkerrors,
  655. .get_enclosure_identifier = mptsas_get_enclosure_identifier,
  656. .get_bay_identifier = mptsas_get_bay_identifier,
  657. .phy_reset = mptsas_phy_reset,
  658. };
  659. static struct scsi_transport_template *mptsas_transport_template;
  660. static int
  661. mptsas_sas_io_unit_pg0(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info)
  662. {
  663. ConfigExtendedPageHeader_t hdr;
  664. CONFIGPARMS cfg;
  665. SasIOUnitPage0_t *buffer;
  666. dma_addr_t dma_handle;
  667. int error, i;
  668. hdr.PageVersion = MPI_SASIOUNITPAGE0_PAGEVERSION;
  669. hdr.ExtPageLength = 0;
  670. hdr.PageNumber = 0;
  671. hdr.Reserved1 = 0;
  672. hdr.Reserved2 = 0;
  673. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  674. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_IO_UNIT;
  675. cfg.cfghdr.ehdr = &hdr;
  676. cfg.physAddr = -1;
  677. cfg.pageAddr = 0;
  678. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  679. cfg.dir = 0; /* read */
  680. cfg.timeout = 10;
  681. error = mpt_config(ioc, &cfg);
  682. if (error)
  683. goto out;
  684. if (!hdr.ExtPageLength) {
  685. error = -ENXIO;
  686. goto out;
  687. }
  688. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  689. &dma_handle);
  690. if (!buffer) {
  691. error = -ENOMEM;
  692. goto out;
  693. }
  694. cfg.physAddr = dma_handle;
  695. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  696. error = mpt_config(ioc, &cfg);
  697. if (error)
  698. goto out_free_consistent;
  699. port_info->num_phys = buffer->NumPhys;
  700. port_info->phy_info = kcalloc(port_info->num_phys,
  701. sizeof(struct mptsas_phyinfo),GFP_KERNEL);
  702. if (!port_info->phy_info) {
  703. error = -ENOMEM;
  704. goto out_free_consistent;
  705. }
  706. if (port_info->num_phys)
  707. port_info->handle =
  708. le16_to_cpu(buffer->PhyData[0].ControllerDevHandle);
  709. for (i = 0; i < port_info->num_phys; i++) {
  710. mptsas_print_phy_data(&buffer->PhyData[i]);
  711. port_info->phy_info[i].phy_id = i;
  712. port_info->phy_info[i].port_id =
  713. buffer->PhyData[i].Port;
  714. port_info->phy_info[i].negotiated_link_rate =
  715. buffer->PhyData[i].NegotiatedLinkRate;
  716. }
  717. out_free_consistent:
  718. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  719. buffer, dma_handle);
  720. out:
  721. return error;
  722. }
  723. static int
  724. mptsas_sas_phy_pg0(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info,
  725. u32 form, u32 form_specific)
  726. {
  727. ConfigExtendedPageHeader_t hdr;
  728. CONFIGPARMS cfg;
  729. SasPhyPage0_t *buffer;
  730. dma_addr_t dma_handle;
  731. int error;
  732. hdr.PageVersion = MPI_SASPHY0_PAGEVERSION;
  733. hdr.ExtPageLength = 0;
  734. hdr.PageNumber = 0;
  735. hdr.Reserved1 = 0;
  736. hdr.Reserved2 = 0;
  737. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  738. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_PHY;
  739. cfg.cfghdr.ehdr = &hdr;
  740. cfg.dir = 0; /* read */
  741. cfg.timeout = 10;
  742. /* Get Phy Pg 0 for each Phy. */
  743. cfg.physAddr = -1;
  744. cfg.pageAddr = form + form_specific;
  745. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  746. error = mpt_config(ioc, &cfg);
  747. if (error)
  748. goto out;
  749. if (!hdr.ExtPageLength) {
  750. error = -ENXIO;
  751. goto out;
  752. }
  753. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  754. &dma_handle);
  755. if (!buffer) {
  756. error = -ENOMEM;
  757. goto out;
  758. }
  759. cfg.physAddr = dma_handle;
  760. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  761. error = mpt_config(ioc, &cfg);
  762. if (error)
  763. goto out_free_consistent;
  764. mptsas_print_phy_pg0(buffer);
  765. phy_info->hw_link_rate = buffer->HwLinkRate;
  766. phy_info->programmed_link_rate = buffer->ProgrammedLinkRate;
  767. phy_info->identify.handle = le16_to_cpu(buffer->OwnerDevHandle);
  768. phy_info->attached.handle = le16_to_cpu(buffer->AttachedDevHandle);
  769. out_free_consistent:
  770. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  771. buffer, dma_handle);
  772. out:
  773. return error;
  774. }
  775. static int
  776. mptsas_sas_device_pg0(MPT_ADAPTER *ioc, struct mptsas_devinfo *device_info,
  777. u32 form, u32 form_specific)
  778. {
  779. ConfigExtendedPageHeader_t hdr;
  780. CONFIGPARMS cfg;
  781. SasDevicePage0_t *buffer;
  782. dma_addr_t dma_handle;
  783. __le64 sas_address;
  784. int error=0;
  785. if (ioc->sas_discovery_runtime &&
  786. mptsas_is_end_device(device_info))
  787. goto out;
  788. hdr.PageVersion = MPI_SASDEVICE0_PAGEVERSION;
  789. hdr.ExtPageLength = 0;
  790. hdr.PageNumber = 0;
  791. hdr.Reserved1 = 0;
  792. hdr.Reserved2 = 0;
  793. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  794. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_DEVICE;
  795. cfg.cfghdr.ehdr = &hdr;
  796. cfg.pageAddr = form + form_specific;
  797. cfg.physAddr = -1;
  798. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  799. cfg.dir = 0; /* read */
  800. cfg.timeout = 10;
  801. memset(device_info, 0, sizeof(struct mptsas_devinfo));
  802. error = mpt_config(ioc, &cfg);
  803. if (error)
  804. goto out;
  805. if (!hdr.ExtPageLength) {
  806. error = -ENXIO;
  807. goto out;
  808. }
  809. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  810. &dma_handle);
  811. if (!buffer) {
  812. error = -ENOMEM;
  813. goto out;
  814. }
  815. cfg.physAddr = dma_handle;
  816. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  817. error = mpt_config(ioc, &cfg);
  818. if (error)
  819. goto out_free_consistent;
  820. mptsas_print_device_pg0(buffer);
  821. device_info->handle = le16_to_cpu(buffer->DevHandle);
  822. device_info->handle_parent = le16_to_cpu(buffer->ParentDevHandle);
  823. device_info->handle_enclosure =
  824. le16_to_cpu(buffer->EnclosureHandle);
  825. device_info->slot = le16_to_cpu(buffer->Slot);
  826. device_info->phy_id = buffer->PhyNum;
  827. device_info->port_id = buffer->PhysicalPort;
  828. device_info->id = buffer->TargetID;
  829. device_info->channel = buffer->Bus;
  830. memcpy(&sas_address, &buffer->SASAddress, sizeof(__le64));
  831. device_info->sas_address = le64_to_cpu(sas_address);
  832. device_info->device_info =
  833. le32_to_cpu(buffer->DeviceInfo);
  834. out_free_consistent:
  835. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  836. buffer, dma_handle);
  837. out:
  838. return error;
  839. }
  840. static int
  841. mptsas_sas_expander_pg0(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info,
  842. u32 form, u32 form_specific)
  843. {
  844. ConfigExtendedPageHeader_t hdr;
  845. CONFIGPARMS cfg;
  846. SasExpanderPage0_t *buffer;
  847. dma_addr_t dma_handle;
  848. int error;
  849. hdr.PageVersion = MPI_SASEXPANDER0_PAGEVERSION;
  850. hdr.ExtPageLength = 0;
  851. hdr.PageNumber = 0;
  852. hdr.Reserved1 = 0;
  853. hdr.Reserved2 = 0;
  854. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  855. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_EXPANDER;
  856. cfg.cfghdr.ehdr = &hdr;
  857. cfg.physAddr = -1;
  858. cfg.pageAddr = form + form_specific;
  859. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  860. cfg.dir = 0; /* read */
  861. cfg.timeout = 10;
  862. memset(port_info, 0, sizeof(struct mptsas_portinfo));
  863. error = mpt_config(ioc, &cfg);
  864. if (error)
  865. goto out;
  866. if (!hdr.ExtPageLength) {
  867. error = -ENXIO;
  868. goto out;
  869. }
  870. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  871. &dma_handle);
  872. if (!buffer) {
  873. error = -ENOMEM;
  874. goto out;
  875. }
  876. cfg.physAddr = dma_handle;
  877. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  878. error = mpt_config(ioc, &cfg);
  879. if (error)
  880. goto out_free_consistent;
  881. /* save config data */
  882. port_info->num_phys = buffer->NumPhys;
  883. port_info->handle = le16_to_cpu(buffer->DevHandle);
  884. port_info->phy_info = kcalloc(port_info->num_phys,
  885. sizeof(struct mptsas_phyinfo),GFP_KERNEL);
  886. if (!port_info->phy_info) {
  887. error = -ENOMEM;
  888. goto out_free_consistent;
  889. }
  890. out_free_consistent:
  891. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  892. buffer, dma_handle);
  893. out:
  894. return error;
  895. }
  896. static int
  897. mptsas_sas_expander_pg1(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info,
  898. u32 form, u32 form_specific)
  899. {
  900. ConfigExtendedPageHeader_t hdr;
  901. CONFIGPARMS cfg;
  902. SasExpanderPage1_t *buffer;
  903. dma_addr_t dma_handle;
  904. int error=0;
  905. if (ioc->sas_discovery_runtime &&
  906. mptsas_is_end_device(&phy_info->attached))
  907. goto out;
  908. hdr.PageVersion = MPI_SASEXPANDER0_PAGEVERSION;
  909. hdr.ExtPageLength = 0;
  910. hdr.PageNumber = 1;
  911. hdr.Reserved1 = 0;
  912. hdr.Reserved2 = 0;
  913. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  914. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_EXPANDER;
  915. cfg.cfghdr.ehdr = &hdr;
  916. cfg.physAddr = -1;
  917. cfg.pageAddr = form + form_specific;
  918. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  919. cfg.dir = 0; /* read */
  920. cfg.timeout = 10;
  921. error = mpt_config(ioc, &cfg);
  922. if (error)
  923. goto out;
  924. if (!hdr.ExtPageLength) {
  925. error = -ENXIO;
  926. goto out;
  927. }
  928. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  929. &dma_handle);
  930. if (!buffer) {
  931. error = -ENOMEM;
  932. goto out;
  933. }
  934. cfg.physAddr = dma_handle;
  935. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  936. error = mpt_config(ioc, &cfg);
  937. if (error)
  938. goto out_free_consistent;
  939. mptsas_print_expander_pg1(buffer);
  940. /* save config data */
  941. phy_info->phy_id = buffer->PhyIdentifier;
  942. phy_info->port_id = buffer->PhysicalPort;
  943. phy_info->negotiated_link_rate = buffer->NegotiatedLinkRate;
  944. phy_info->programmed_link_rate = buffer->ProgrammedLinkRate;
  945. phy_info->hw_link_rate = buffer->HwLinkRate;
  946. phy_info->identify.handle = le16_to_cpu(buffer->OwnerDevHandle);
  947. phy_info->attached.handle = le16_to_cpu(buffer->AttachedDevHandle);
  948. out_free_consistent:
  949. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  950. buffer, dma_handle);
  951. out:
  952. return error;
  953. }
  954. static void
  955. mptsas_parse_device_info(struct sas_identify *identify,
  956. struct mptsas_devinfo *device_info)
  957. {
  958. u16 protocols;
  959. identify->sas_address = device_info->sas_address;
  960. identify->phy_identifier = device_info->phy_id;
  961. /*
  962. * Fill in Phy Initiator Port Protocol.
  963. * Bits 6:3, more than one bit can be set, fall through cases.
  964. */
  965. protocols = device_info->device_info & 0x78;
  966. identify->initiator_port_protocols = 0;
  967. if (protocols & MPI_SAS_DEVICE_INFO_SSP_INITIATOR)
  968. identify->initiator_port_protocols |= SAS_PROTOCOL_SSP;
  969. if (protocols & MPI_SAS_DEVICE_INFO_STP_INITIATOR)
  970. identify->initiator_port_protocols |= SAS_PROTOCOL_STP;
  971. if (protocols & MPI_SAS_DEVICE_INFO_SMP_INITIATOR)
  972. identify->initiator_port_protocols |= SAS_PROTOCOL_SMP;
  973. if (protocols & MPI_SAS_DEVICE_INFO_SATA_HOST)
  974. identify->initiator_port_protocols |= SAS_PROTOCOL_SATA;
  975. /*
  976. * Fill in Phy Target Port Protocol.
  977. * Bits 10:7, more than one bit can be set, fall through cases.
  978. */
  979. protocols = device_info->device_info & 0x780;
  980. identify->target_port_protocols = 0;
  981. if (protocols & MPI_SAS_DEVICE_INFO_SSP_TARGET)
  982. identify->target_port_protocols |= SAS_PROTOCOL_SSP;
  983. if (protocols & MPI_SAS_DEVICE_INFO_STP_TARGET)
  984. identify->target_port_protocols |= SAS_PROTOCOL_STP;
  985. if (protocols & MPI_SAS_DEVICE_INFO_SMP_TARGET)
  986. identify->target_port_protocols |= SAS_PROTOCOL_SMP;
  987. if (protocols & MPI_SAS_DEVICE_INFO_SATA_DEVICE)
  988. identify->target_port_protocols |= SAS_PROTOCOL_SATA;
  989. /*
  990. * Fill in Attached device type.
  991. */
  992. switch (device_info->device_info &
  993. MPI_SAS_DEVICE_INFO_MASK_DEVICE_TYPE) {
  994. case MPI_SAS_DEVICE_INFO_NO_DEVICE:
  995. identify->device_type = SAS_PHY_UNUSED;
  996. break;
  997. case MPI_SAS_DEVICE_INFO_END_DEVICE:
  998. identify->device_type = SAS_END_DEVICE;
  999. break;
  1000. case MPI_SAS_DEVICE_INFO_EDGE_EXPANDER:
  1001. identify->device_type = SAS_EDGE_EXPANDER_DEVICE;
  1002. break;
  1003. case MPI_SAS_DEVICE_INFO_FANOUT_EXPANDER:
  1004. identify->device_type = SAS_FANOUT_EXPANDER_DEVICE;
  1005. break;
  1006. }
  1007. }
  1008. static int mptsas_probe_one_phy(struct device *dev,
  1009. struct mptsas_phyinfo *phy_info, int index, int local)
  1010. {
  1011. MPT_ADAPTER *ioc;
  1012. struct sas_phy *phy;
  1013. int error;
  1014. if (!dev)
  1015. return -ENODEV;
  1016. if (!phy_info->phy) {
  1017. phy = sas_phy_alloc(dev, index);
  1018. if (!phy)
  1019. return -ENOMEM;
  1020. } else
  1021. phy = phy_info->phy;
  1022. phy->port_identifier = phy_info->port_id;
  1023. mptsas_parse_device_info(&phy->identify, &phy_info->identify);
  1024. /*
  1025. * Set Negotiated link rate.
  1026. */
  1027. switch (phy_info->negotiated_link_rate) {
  1028. case MPI_SAS_IOUNIT0_RATE_PHY_DISABLED:
  1029. phy->negotiated_linkrate = SAS_PHY_DISABLED;
  1030. break;
  1031. case MPI_SAS_IOUNIT0_RATE_FAILED_SPEED_NEGOTIATION:
  1032. phy->negotiated_linkrate = SAS_LINK_RATE_FAILED;
  1033. break;
  1034. case MPI_SAS_IOUNIT0_RATE_1_5:
  1035. phy->negotiated_linkrate = SAS_LINK_RATE_1_5_GBPS;
  1036. break;
  1037. case MPI_SAS_IOUNIT0_RATE_3_0:
  1038. phy->negotiated_linkrate = SAS_LINK_RATE_3_0_GBPS;
  1039. break;
  1040. case MPI_SAS_IOUNIT0_RATE_SATA_OOB_COMPLETE:
  1041. case MPI_SAS_IOUNIT0_RATE_UNKNOWN:
  1042. default:
  1043. phy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
  1044. break;
  1045. }
  1046. /*
  1047. * Set Max hardware link rate.
  1048. */
  1049. switch (phy_info->hw_link_rate & MPI_SAS_PHY0_PRATE_MAX_RATE_MASK) {
  1050. case MPI_SAS_PHY0_HWRATE_MAX_RATE_1_5:
  1051. phy->maximum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
  1052. break;
  1053. case MPI_SAS_PHY0_PRATE_MAX_RATE_3_0:
  1054. phy->maximum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
  1055. break;
  1056. default:
  1057. break;
  1058. }
  1059. /*
  1060. * Set Max programmed link rate.
  1061. */
  1062. switch (phy_info->programmed_link_rate &
  1063. MPI_SAS_PHY0_PRATE_MAX_RATE_MASK) {
  1064. case MPI_SAS_PHY0_PRATE_MAX_RATE_1_5:
  1065. phy->maximum_linkrate = SAS_LINK_RATE_1_5_GBPS;
  1066. break;
  1067. case MPI_SAS_PHY0_PRATE_MAX_RATE_3_0:
  1068. phy->maximum_linkrate = SAS_LINK_RATE_3_0_GBPS;
  1069. break;
  1070. default:
  1071. break;
  1072. }
  1073. /*
  1074. * Set Min hardware link rate.
  1075. */
  1076. switch (phy_info->hw_link_rate & MPI_SAS_PHY0_HWRATE_MIN_RATE_MASK) {
  1077. case MPI_SAS_PHY0_HWRATE_MIN_RATE_1_5:
  1078. phy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
  1079. break;
  1080. case MPI_SAS_PHY0_PRATE_MIN_RATE_3_0:
  1081. phy->minimum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
  1082. break;
  1083. default:
  1084. break;
  1085. }
  1086. /*
  1087. * Set Min programmed link rate.
  1088. */
  1089. switch (phy_info->programmed_link_rate &
  1090. MPI_SAS_PHY0_PRATE_MIN_RATE_MASK) {
  1091. case MPI_SAS_PHY0_PRATE_MIN_RATE_1_5:
  1092. phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
  1093. break;
  1094. case MPI_SAS_PHY0_PRATE_MIN_RATE_3_0:
  1095. phy->minimum_linkrate = SAS_LINK_RATE_3_0_GBPS;
  1096. break;
  1097. default:
  1098. break;
  1099. }
  1100. if (!phy_info->phy) {
  1101. if (local)
  1102. phy->local_attached = 1;
  1103. error = sas_phy_add(phy);
  1104. if (error) {
  1105. sas_phy_free(phy);
  1106. return error;
  1107. }
  1108. phy_info->phy = phy;
  1109. }
  1110. if ((phy_info->attached.handle) &&
  1111. (!phy_info->rphy)) {
  1112. struct sas_rphy *rphy;
  1113. struct sas_identify identify;
  1114. ioc = phy_to_ioc(phy_info->phy);
  1115. /*
  1116. * Let the hotplug_work thread handle processing
  1117. * the adding/removing of devices that occur
  1118. * after start of day.
  1119. */
  1120. if (ioc->sas_discovery_runtime &&
  1121. mptsas_is_end_device(&phy_info->attached))
  1122. return 0;
  1123. mptsas_parse_device_info(&identify, &phy_info->attached);
  1124. switch (identify.device_type) {
  1125. case SAS_END_DEVICE:
  1126. rphy = sas_end_device_alloc(phy);
  1127. break;
  1128. case SAS_EDGE_EXPANDER_DEVICE:
  1129. case SAS_FANOUT_EXPANDER_DEVICE:
  1130. rphy = sas_expander_alloc(phy, identify.device_type);
  1131. break;
  1132. default:
  1133. rphy = NULL;
  1134. break;
  1135. }
  1136. if (!rphy)
  1137. return 0; /* non-fatal: an rphy can be added later */
  1138. rphy->identify = identify;
  1139. error = sas_rphy_add(rphy);
  1140. if (error) {
  1141. sas_rphy_free(rphy);
  1142. return error;
  1143. }
  1144. phy_info->rphy = rphy;
  1145. }
  1146. return 0;
  1147. }
  1148. static int
  1149. mptsas_probe_hba_phys(MPT_ADAPTER *ioc)
  1150. {
  1151. struct mptsas_portinfo *port_info, *hba;
  1152. u32 handle = 0xFFFF;
  1153. int error = -ENOMEM, i;
  1154. hba = kzalloc(sizeof(*port_info), GFP_KERNEL);
  1155. if (! hba)
  1156. goto out;
  1157. error = mptsas_sas_io_unit_pg0(ioc, hba);
  1158. if (error)
  1159. goto out_free_port_info;
  1160. mutex_lock(&ioc->sas_topology_mutex);
  1161. port_info = mptsas_find_portinfo_by_handle(ioc, hba->handle);
  1162. if (!port_info) {
  1163. port_info = hba;
  1164. list_add_tail(&port_info->list, &ioc->sas_topology);
  1165. } else {
  1166. port_info->handle = hba->handle;
  1167. for (i = 0; i < hba->num_phys; i++)
  1168. port_info->phy_info[i].negotiated_link_rate =
  1169. hba->phy_info[i].negotiated_link_rate;
  1170. if (hba->phy_info)
  1171. kfree(hba->phy_info);
  1172. kfree(hba);
  1173. hba = NULL;
  1174. }
  1175. mutex_unlock(&ioc->sas_topology_mutex);
  1176. ioc->num_ports = port_info->num_phys;
  1177. for (i = 0; i < port_info->num_phys; i++) {
  1178. mptsas_sas_phy_pg0(ioc, &port_info->phy_info[i],
  1179. (MPI_SAS_PHY_PGAD_FORM_PHY_NUMBER <<
  1180. MPI_SAS_PHY_PGAD_FORM_SHIFT), i);
  1181. mptsas_sas_device_pg0(ioc, &port_info->phy_info[i].identify,
  1182. (MPI_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE <<
  1183. MPI_SAS_DEVICE_PGAD_FORM_SHIFT), handle);
  1184. port_info->phy_info[i].identify.phy_id =
  1185. port_info->phy_info[i].phy_id;
  1186. handle = port_info->phy_info[i].identify.handle;
  1187. if (port_info->phy_info[i].attached.handle) {
  1188. mptsas_sas_device_pg0(ioc,
  1189. &port_info->phy_info[i].attached,
  1190. (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
  1191. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  1192. port_info->phy_info[i].attached.handle);
  1193. }
  1194. mptsas_probe_one_phy(&ioc->sh->shost_gendev,
  1195. &port_info->phy_info[i], ioc->sas_index, 1);
  1196. ioc->sas_index++;
  1197. }
  1198. return 0;
  1199. out_free_port_info:
  1200. if (hba)
  1201. kfree(hba);
  1202. out:
  1203. return error;
  1204. }
  1205. static int
  1206. mptsas_probe_expander_phys(MPT_ADAPTER *ioc, u32 *handle)
  1207. {
  1208. struct mptsas_portinfo *port_info, *p, *ex;
  1209. int error = -ENOMEM, i, j;
  1210. ex = kzalloc(sizeof(*port_info), GFP_KERNEL);
  1211. if (!ex)
  1212. goto out;
  1213. error = mptsas_sas_expander_pg0(ioc, ex,
  1214. (MPI_SAS_EXPAND_PGAD_FORM_GET_NEXT_HANDLE <<
  1215. MPI_SAS_EXPAND_PGAD_FORM_SHIFT), *handle);
  1216. if (error)
  1217. goto out_free_port_info;
  1218. *handle = ex->handle;
  1219. mutex_lock(&ioc->sas_topology_mutex);
  1220. port_info = mptsas_find_portinfo_by_handle(ioc, *handle);
  1221. if (!port_info) {
  1222. port_info = ex;
  1223. list_add_tail(&port_info->list, &ioc->sas_topology);
  1224. } else {
  1225. port_info->handle = ex->handle;
  1226. if (ex->phy_info)
  1227. kfree(ex->phy_info);
  1228. kfree(ex);
  1229. ex = NULL;
  1230. }
  1231. mutex_unlock(&ioc->sas_topology_mutex);
  1232. for (i = 0; i < port_info->num_phys; i++) {
  1233. struct device *parent;
  1234. mptsas_sas_expander_pg1(ioc, &port_info->phy_info[i],
  1235. (MPI_SAS_EXPAND_PGAD_FORM_HANDLE_PHY_NUM <<
  1236. MPI_SAS_EXPAND_PGAD_FORM_SHIFT), (i << 16) + *handle);
  1237. if (port_info->phy_info[i].identify.handle) {
  1238. mptsas_sas_device_pg0(ioc,
  1239. &port_info->phy_info[i].identify,
  1240. (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
  1241. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  1242. port_info->phy_info[i].identify.handle);
  1243. port_info->phy_info[i].identify.phy_id =
  1244. port_info->phy_info[i].phy_id;
  1245. }
  1246. if (port_info->phy_info[i].attached.handle) {
  1247. mptsas_sas_device_pg0(ioc,
  1248. &port_info->phy_info[i].attached,
  1249. (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
  1250. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  1251. port_info->phy_info[i].attached.handle);
  1252. port_info->phy_info[i].attached.phy_id =
  1253. port_info->phy_info[i].phy_id;
  1254. }
  1255. /*
  1256. * If we find a parent port handle this expander is
  1257. * attached to another expander, else it hangs of the
  1258. * HBA phys.
  1259. */
  1260. parent = &ioc->sh->shost_gendev;
  1261. mutex_lock(&ioc->sas_topology_mutex);
  1262. list_for_each_entry(p, &ioc->sas_topology, list) {
  1263. for (j = 0; j < p->num_phys; j++) {
  1264. if (port_info->phy_info[i].identify.handle ==
  1265. p->phy_info[j].attached.handle)
  1266. parent = &p->phy_info[j].rphy->dev;
  1267. }
  1268. }
  1269. mutex_unlock(&ioc->sas_topology_mutex);
  1270. mptsas_probe_one_phy(parent, &port_info->phy_info[i],
  1271. ioc->sas_index, 0);
  1272. ioc->sas_index++;
  1273. }
  1274. return 0;
  1275. out_free_port_info:
  1276. if (ex) {
  1277. if (ex->phy_info)
  1278. kfree(ex->phy_info);
  1279. kfree(ex);
  1280. }
  1281. out:
  1282. return error;
  1283. }
  1284. /*
  1285. * mptsas_delete_expander_phys
  1286. *
  1287. *
  1288. * This will traverse topology, and remove expanders
  1289. * that are no longer present
  1290. */
  1291. static void
  1292. mptsas_delete_expander_phys(MPT_ADAPTER *ioc)
  1293. {
  1294. struct mptsas_portinfo buffer;
  1295. struct mptsas_portinfo *port_info, *n, *parent;
  1296. int i;
  1297. mutex_lock(&ioc->sas_topology_mutex);
  1298. list_for_each_entry_safe(port_info, n, &ioc->sas_topology, list) {
  1299. if (port_info->phy_info &&
  1300. (!(port_info->phy_info[0].identify.device_info &
  1301. MPI_SAS_DEVICE_INFO_SMP_TARGET)))
  1302. continue;
  1303. if (mptsas_sas_expander_pg0(ioc, &buffer,
  1304. (MPI_SAS_EXPAND_PGAD_FORM_HANDLE <<
  1305. MPI_SAS_EXPAND_PGAD_FORM_SHIFT), port_info->handle)) {
  1306. /*
  1307. * Obtain the port_info instance to the parent port
  1308. */
  1309. parent = mptsas_find_portinfo_by_handle(ioc,
  1310. port_info->phy_info[0].identify.handle_parent);
  1311. if (!parent)
  1312. goto next_port;
  1313. /*
  1314. * Delete rphys in the parent that point
  1315. * to this expander. The transport layer will
  1316. * cleanup all the children.
  1317. */
  1318. for (i = 0; i < parent->num_phys; i++) {
  1319. if ((!parent->phy_info[i].rphy) ||
  1320. (parent->phy_info[i].attached.sas_address !=
  1321. port_info->phy_info[i].identify.sas_address))
  1322. continue;
  1323. sas_rphy_delete(parent->phy_info[i].rphy);
  1324. memset(&parent->phy_info[i].attached, 0,
  1325. sizeof(struct mptsas_devinfo));
  1326. parent->phy_info[i].rphy = NULL;
  1327. parent->phy_info[i].starget = NULL;
  1328. }
  1329. next_port:
  1330. list_del(&port_info->list);
  1331. if (port_info->phy_info)
  1332. kfree(port_info->phy_info);
  1333. kfree(port_info);
  1334. }
  1335. /*
  1336. * Free this memory allocated from inside
  1337. * mptsas_sas_expander_pg0
  1338. */
  1339. if (buffer.phy_info)
  1340. kfree(buffer.phy_info);
  1341. }
  1342. mutex_unlock(&ioc->sas_topology_mutex);
  1343. }
  1344. /*
  1345. * Start of day discovery
  1346. */
  1347. static void
  1348. mptsas_scan_sas_topology(MPT_ADAPTER *ioc)
  1349. {
  1350. u32 handle = 0xFFFF;
  1351. int i;
  1352. mutex_lock(&ioc->sas_discovery_mutex);
  1353. mptsas_probe_hba_phys(ioc);
  1354. while (!mptsas_probe_expander_phys(ioc, &handle))
  1355. ;
  1356. /*
  1357. Reporting RAID volumes.
  1358. */
  1359. if (!ioc->raid_data.pIocPg2)
  1360. goto out;
  1361. if (!ioc->raid_data.pIocPg2->NumActiveVolumes)
  1362. goto out;
  1363. for (i=0; i<ioc->raid_data.pIocPg2->NumActiveVolumes; i++) {
  1364. scsi_add_device(ioc->sh, ioc->num_ports,
  1365. ioc->raid_data.pIocPg2->RaidVolume[i].VolumeID, 0);
  1366. }
  1367. out:
  1368. mutex_unlock(&ioc->sas_discovery_mutex);
  1369. }
  1370. /*
  1371. * Work queue thread to handle Runtime discovery
  1372. * Mere purpose is the hot add/delete of expanders
  1373. */
  1374. static void
  1375. mptscsih_discovery_work(void * arg)
  1376. {
  1377. struct mptsas_discovery_event *ev = arg;
  1378. MPT_ADAPTER *ioc = ev->ioc;
  1379. u32 handle = 0xFFFF;
  1380. mutex_lock(&ioc->sas_discovery_mutex);
  1381. ioc->sas_discovery_runtime=1;
  1382. mptsas_delete_expander_phys(ioc);
  1383. mptsas_probe_hba_phys(ioc);
  1384. while (!mptsas_probe_expander_phys(ioc, &handle))
  1385. ;
  1386. kfree(ev);
  1387. ioc->sas_discovery_runtime=0;
  1388. mutex_unlock(&ioc->sas_discovery_mutex);
  1389. }
  1390. static struct mptsas_phyinfo *
  1391. mptsas_find_phyinfo_by_parent(MPT_ADAPTER *ioc, u16 parent_handle, u8 phy_id)
  1392. {
  1393. struct mptsas_portinfo *port_info;
  1394. struct mptsas_devinfo device_info;
  1395. struct mptsas_phyinfo *phy_info = NULL;
  1396. int i, error;
  1397. /*
  1398. * Retrieve the parent sas_address
  1399. */
  1400. error = mptsas_sas_device_pg0(ioc, &device_info,
  1401. (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
  1402. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  1403. parent_handle);
  1404. if (error)
  1405. return NULL;
  1406. /*
  1407. * The phy_info structures are never deallocated during lifetime of
  1408. * a host, so the code below is safe without additional refcounting.
  1409. */
  1410. mutex_lock(&ioc->sas_topology_mutex);
  1411. list_for_each_entry(port_info, &ioc->sas_topology, list) {
  1412. for (i = 0; i < port_info->num_phys; i++) {
  1413. if (port_info->phy_info[i].identify.sas_address ==
  1414. device_info.sas_address &&
  1415. port_info->phy_info[i].phy_id == phy_id) {
  1416. phy_info = &port_info->phy_info[i];
  1417. break;
  1418. }
  1419. }
  1420. }
  1421. mutex_unlock(&ioc->sas_topology_mutex);
  1422. return phy_info;
  1423. }
  1424. static struct mptsas_phyinfo *
  1425. mptsas_find_phyinfo_by_target(MPT_ADAPTER *ioc, u32 id)
  1426. {
  1427. struct mptsas_portinfo *port_info;
  1428. struct mptsas_phyinfo *phy_info = NULL;
  1429. int i;
  1430. /*
  1431. * The phy_info structures are never deallocated during lifetime of
  1432. * a host, so the code below is safe without additional refcounting.
  1433. */
  1434. mutex_lock(&ioc->sas_topology_mutex);
  1435. list_for_each_entry(port_info, &ioc->sas_topology, list) {
  1436. for (i = 0; i < port_info->num_phys; i++)
  1437. if (mptsas_is_end_device(&port_info->phy_info[i].attached))
  1438. if (port_info->phy_info[i].attached.id == id) {
  1439. phy_info = &port_info->phy_info[i];
  1440. break;
  1441. }
  1442. }
  1443. mutex_unlock(&ioc->sas_topology_mutex);
  1444. return phy_info;
  1445. }
  1446. /*
  1447. * Work queue thread to clear the persitency table
  1448. */
  1449. static void
  1450. mptscsih_sas_persist_clear_table(void * arg)
  1451. {
  1452. MPT_ADAPTER *ioc = (MPT_ADAPTER *)arg;
  1453. mptbase_sas_persist_operation(ioc, MPI_SAS_OP_CLEAR_NOT_PRESENT);
  1454. }
  1455. static void
  1456. mptsas_reprobe_lun(struct scsi_device *sdev, void *data)
  1457. {
  1458. sdev->no_uld_attach = data ? 1 : 0;
  1459. scsi_device_reprobe(sdev);
  1460. }
  1461. static void
  1462. mptsas_reprobe_target(struct scsi_target *starget, int uld_attach)
  1463. {
  1464. starget_for_each_device(starget, uld_attach ? (void *)1 : NULL,
  1465. mptsas_reprobe_lun);
  1466. }
  1467. /*
  1468. * Work queue thread to handle SAS hotplug events
  1469. */
  1470. static void
  1471. mptsas_hotplug_work(void *arg)
  1472. {
  1473. struct mptsas_hotplug_event *ev = arg;
  1474. MPT_ADAPTER *ioc = ev->ioc;
  1475. struct mptsas_phyinfo *phy_info;
  1476. struct sas_rphy *rphy;
  1477. struct scsi_device *sdev;
  1478. struct sas_identify identify;
  1479. char *ds = NULL;
  1480. struct mptsas_devinfo sas_device;
  1481. VirtTarget *vtarget;
  1482. mutex_lock(&ioc->sas_discovery_mutex);
  1483. switch (ev->event_type) {
  1484. case MPTSAS_DEL_DEVICE:
  1485. phy_info = mptsas_find_phyinfo_by_target(ioc, ev->id);
  1486. /*
  1487. * Sanity checks, for non-existing phys and remote rphys.
  1488. */
  1489. if (!phy_info)
  1490. break;
  1491. if (!phy_info->rphy)
  1492. break;
  1493. if (phy_info->starget) {
  1494. vtarget = phy_info->starget->hostdata;
  1495. if (!vtarget)
  1496. break;
  1497. /*
  1498. * Handling RAID components
  1499. */
  1500. if (ev->phys_disk_num_valid) {
  1501. vtarget->target_id = ev->phys_disk_num;
  1502. vtarget->tflags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
  1503. mptsas_reprobe_target(vtarget->starget, 1);
  1504. break;
  1505. }
  1506. }
  1507. if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_SSP_TARGET)
  1508. ds = "ssp";
  1509. if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_STP_TARGET)
  1510. ds = "stp";
  1511. if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_SATA_DEVICE)
  1512. ds = "sata";
  1513. printk(MYIOC_s_INFO_FMT
  1514. "removing %s device, channel %d, id %d, phy %d\n",
  1515. ioc->name, ds, ev->channel, ev->id, phy_info->phy_id);
  1516. sas_rphy_delete(phy_info->rphy);
  1517. memset(&phy_info->attached, 0, sizeof(struct mptsas_devinfo));
  1518. phy_info->rphy = NULL;
  1519. phy_info->starget = NULL;
  1520. break;
  1521. case MPTSAS_ADD_DEVICE:
  1522. if (ev->phys_disk_num_valid)
  1523. mpt_findImVolumes(ioc);
  1524. /*
  1525. * Refresh sas device pg0 data
  1526. */
  1527. if (mptsas_sas_device_pg0(ioc, &sas_device,
  1528. (MPI_SAS_DEVICE_PGAD_FORM_BUS_TARGET_ID <<
  1529. MPI_SAS_DEVICE_PGAD_FORM_SHIFT), ev->id))
  1530. break;
  1531. phy_info = mptsas_find_phyinfo_by_parent(ioc,
  1532. sas_device.handle_parent, sas_device.phy_id);
  1533. if (!phy_info) {
  1534. u32 handle = 0xFFFF;
  1535. /*
  1536. * Its possible when an expander has been hot added
  1537. * containing attached devices, the sas firmware
  1538. * may send a RC_ADDED event prior to the
  1539. * DISCOVERY STOP event. If that occurs, our
  1540. * view of the topology in the driver in respect to this
  1541. * expander might of not been setup, and we hit this
  1542. * condition.
  1543. * Therefore, this code kicks off discovery to
  1544. * refresh the data.
  1545. * Then again, we check whether the parent phy has
  1546. * been created.
  1547. */
  1548. ioc->sas_discovery_runtime=1;
  1549. mptsas_delete_expander_phys(ioc);
  1550. mptsas_probe_hba_phys(ioc);
  1551. while (!mptsas_probe_expander_phys(ioc, &handle))
  1552. ;
  1553. ioc->sas_discovery_runtime=0;
  1554. phy_info = mptsas_find_phyinfo_by_parent(ioc,
  1555. sas_device.handle_parent, sas_device.phy_id);
  1556. if (!phy_info)
  1557. break;
  1558. }
  1559. if (phy_info->starget) {
  1560. vtarget = phy_info->starget->hostdata;
  1561. if (!vtarget)
  1562. break;
  1563. /*
  1564. * Handling RAID components
  1565. */
  1566. if (vtarget->tflags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
  1567. vtarget->tflags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
  1568. vtarget->target_id = ev->id;
  1569. mptsas_reprobe_target(phy_info->starget, 0);
  1570. }
  1571. break;
  1572. }
  1573. if (phy_info->rphy)
  1574. break;
  1575. memcpy(&phy_info->attached, &sas_device,
  1576. sizeof(struct mptsas_devinfo));
  1577. if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_SSP_TARGET)
  1578. ds = "ssp";
  1579. if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_STP_TARGET)
  1580. ds = "stp";
  1581. if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_SATA_DEVICE)
  1582. ds = "sata";
  1583. printk(MYIOC_s_INFO_FMT
  1584. "attaching %s device, channel %d, id %d, phy %d\n",
  1585. ioc->name, ds, ev->channel, ev->id, ev->phy_id);
  1586. mptsas_parse_device_info(&identify, &phy_info->attached);
  1587. switch (identify.device_type) {
  1588. case SAS_END_DEVICE:
  1589. rphy = sas_end_device_alloc(phy_info->phy);
  1590. break;
  1591. case SAS_EDGE_EXPANDER_DEVICE:
  1592. case SAS_FANOUT_EXPANDER_DEVICE:
  1593. rphy = sas_expander_alloc(phy_info->phy, identify.device_type);
  1594. break;
  1595. default:
  1596. rphy = NULL;
  1597. break;
  1598. }
  1599. if (!rphy)
  1600. break; /* non-fatal: an rphy can be added later */
  1601. rphy->identify = identify;
  1602. if (sas_rphy_add(rphy)) {
  1603. sas_rphy_free(rphy);
  1604. break;
  1605. }
  1606. phy_info->rphy = rphy;
  1607. break;
  1608. case MPTSAS_ADD_RAID:
  1609. sdev = scsi_device_lookup(
  1610. ioc->sh,
  1611. ioc->num_ports,
  1612. ev->id,
  1613. 0);
  1614. if (sdev) {
  1615. scsi_device_put(sdev);
  1616. break;
  1617. }
  1618. printk(MYIOC_s_INFO_FMT
  1619. "attaching raid volume, channel %d, id %d\n",
  1620. ioc->name, ioc->num_ports, ev->id);
  1621. scsi_add_device(ioc->sh,
  1622. ioc->num_ports,
  1623. ev->id,
  1624. 0);
  1625. mpt_findImVolumes(ioc);
  1626. break;
  1627. case MPTSAS_DEL_RAID:
  1628. sdev = scsi_device_lookup(
  1629. ioc->sh,
  1630. ioc->num_ports,
  1631. ev->id,
  1632. 0);
  1633. if (!sdev)
  1634. break;
  1635. printk(MYIOC_s_INFO_FMT
  1636. "removing raid volume, channel %d, id %d\n",
  1637. ioc->name, ioc->num_ports, ev->id);
  1638. scsi_remove_device(sdev);
  1639. scsi_device_put(sdev);
  1640. mpt_findImVolumes(ioc);
  1641. break;
  1642. case MPTSAS_IGNORE_EVENT:
  1643. default:
  1644. break;
  1645. }
  1646. kfree(ev);
  1647. mutex_unlock(&ioc->sas_discovery_mutex);
  1648. }
  1649. static void
  1650. mptscsih_send_sas_event(MPT_ADAPTER *ioc,
  1651. EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *sas_event_data)
  1652. {
  1653. struct mptsas_hotplug_event *ev;
  1654. u32 device_info = le32_to_cpu(sas_event_data->DeviceInfo);
  1655. __le64 sas_address;
  1656. if ((device_info &
  1657. (MPI_SAS_DEVICE_INFO_SSP_TARGET |
  1658. MPI_SAS_DEVICE_INFO_STP_TARGET |
  1659. MPI_SAS_DEVICE_INFO_SATA_DEVICE )) == 0)
  1660. return;
  1661. switch (sas_event_data->ReasonCode) {
  1662. case MPI_EVENT_SAS_DEV_STAT_RC_ADDED:
  1663. case MPI_EVENT_SAS_DEV_STAT_RC_NOT_RESPONDING:
  1664. ev = kmalloc(sizeof(*ev), GFP_ATOMIC);
  1665. if (!ev) {
  1666. printk(KERN_WARNING "mptsas: lost hotplug event\n");
  1667. break;
  1668. }
  1669. INIT_WORK(&ev->work, mptsas_hotplug_work, ev);
  1670. ev->ioc = ioc;
  1671. ev->handle = le16_to_cpu(sas_event_data->DevHandle);
  1672. ev->parent_handle =
  1673. le16_to_cpu(sas_event_data->ParentDevHandle);
  1674. ev->channel = sas_event_data->Bus;
  1675. ev->id = sas_event_data->TargetID;
  1676. ev->phy_id = sas_event_data->PhyNum;
  1677. memcpy(&sas_address, &sas_event_data->SASAddress,
  1678. sizeof(__le64));
  1679. ev->sas_address = le64_to_cpu(sas_address);
  1680. ev->device_info = device_info;
  1681. if (sas_event_data->ReasonCode &
  1682. MPI_EVENT_SAS_DEV_STAT_RC_ADDED)
  1683. ev->event_type = MPTSAS_ADD_DEVICE;
  1684. else
  1685. ev->event_type = MPTSAS_DEL_DEVICE;
  1686. schedule_work(&ev->work);
  1687. break;
  1688. case MPI_EVENT_SAS_DEV_STAT_RC_NO_PERSIST_ADDED:
  1689. /*
  1690. * Persistent table is full.
  1691. */
  1692. INIT_WORK(&ioc->mptscsih_persistTask,
  1693. mptscsih_sas_persist_clear_table,
  1694. (void *)ioc);
  1695. schedule_work(&ioc->mptscsih_persistTask);
  1696. break;
  1697. case MPI_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
  1698. /* TODO */
  1699. case MPI_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
  1700. /* TODO */
  1701. default:
  1702. break;
  1703. }
  1704. }
  1705. static void
  1706. mptscsih_send_raid_event(MPT_ADAPTER *ioc,
  1707. EVENT_DATA_RAID *raid_event_data)
  1708. {
  1709. struct mptsas_hotplug_event *ev;
  1710. int status = le32_to_cpu(raid_event_data->SettingsStatus);
  1711. int state = (status >> 8) & 0xff;
  1712. if (ioc->bus_type != SAS)
  1713. return;
  1714. ev = kmalloc(sizeof(*ev), GFP_ATOMIC);
  1715. if (!ev) {
  1716. printk(KERN_WARNING "mptsas: lost hotplug event\n");
  1717. return;
  1718. }
  1719. memset(ev,0,sizeof(struct mptsas_hotplug_event));
  1720. INIT_WORK(&ev->work, mptsas_hotplug_work, ev);
  1721. ev->ioc = ioc;
  1722. ev->id = raid_event_data->VolumeID;
  1723. ev->event_type = MPTSAS_IGNORE_EVENT;
  1724. switch (raid_event_data->ReasonCode) {
  1725. case MPI_EVENT_RAID_RC_PHYSDISK_DELETED:
  1726. ev->event_type = MPTSAS_ADD_DEVICE;
  1727. break;
  1728. case MPI_EVENT_RAID_RC_PHYSDISK_CREATED:
  1729. ioc->raid_data.isRaid = 1;
  1730. ev->phys_disk_num_valid = 1;
  1731. ev->phys_disk_num = raid_event_data->PhysDiskNum;
  1732. ev->event_type = MPTSAS_DEL_DEVICE;
  1733. break;
  1734. case MPI_EVENT_RAID_RC_PHYSDISK_STATUS_CHANGED:
  1735. switch (state) {
  1736. case MPI_PD_STATE_ONLINE:
  1737. ioc->raid_data.isRaid = 1;
  1738. ev->phys_disk_num_valid = 1;
  1739. ev->phys_disk_num = raid_event_data->PhysDiskNum;
  1740. ev->event_type = MPTSAS_ADD_DEVICE;
  1741. break;
  1742. case MPI_PD_STATE_MISSING:
  1743. case MPI_PD_STATE_NOT_COMPATIBLE:
  1744. case MPI_PD_STATE_OFFLINE_AT_HOST_REQUEST:
  1745. case MPI_PD_STATE_FAILED_AT_HOST_REQUEST:
  1746. case MPI_PD_STATE_OFFLINE_FOR_ANOTHER_REASON:
  1747. ev->event_type = MPTSAS_DEL_DEVICE;
  1748. break;
  1749. default:
  1750. break;
  1751. }
  1752. break;
  1753. case MPI_EVENT_RAID_RC_VOLUME_DELETED:
  1754. ev->event_type = MPTSAS_DEL_RAID;
  1755. break;
  1756. case MPI_EVENT_RAID_RC_VOLUME_CREATED:
  1757. ev->event_type = MPTSAS_ADD_RAID;
  1758. break;
  1759. case MPI_EVENT_RAID_RC_VOLUME_STATUS_CHANGED:
  1760. switch (state) {
  1761. case MPI_RAIDVOL0_STATUS_STATE_FAILED:
  1762. case MPI_RAIDVOL0_STATUS_STATE_MISSING:
  1763. ev->event_type = MPTSAS_DEL_RAID;
  1764. break;
  1765. case MPI_RAIDVOL0_STATUS_STATE_OPTIMAL:
  1766. case MPI_RAIDVOL0_STATUS_STATE_DEGRADED:
  1767. ev->event_type = MPTSAS_ADD_RAID;
  1768. break;
  1769. default:
  1770. break;
  1771. }
  1772. break;
  1773. default:
  1774. break;
  1775. }
  1776. schedule_work(&ev->work);
  1777. }
  1778. static void
  1779. mptscsih_send_discovery(MPT_ADAPTER *ioc,
  1780. EVENT_DATA_SAS_DISCOVERY *discovery_data)
  1781. {
  1782. struct mptsas_discovery_event *ev;
  1783. /*
  1784. * DiscoveryStatus
  1785. *
  1786. * This flag will be non-zero when firmware
  1787. * kicks off discovery, and return to zero
  1788. * once its completed.
  1789. */
  1790. if (discovery_data->DiscoveryStatus)
  1791. return;
  1792. ev = kmalloc(sizeof(*ev), GFP_ATOMIC);
  1793. if (!ev)
  1794. return;
  1795. memset(ev,0,sizeof(struct mptsas_discovery_event));
  1796. INIT_WORK(&ev->work, mptscsih_discovery_work, ev);
  1797. ev->ioc = ioc;
  1798. schedule_work(&ev->work);
  1799. };
  1800. static int
  1801. mptsas_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *reply)
  1802. {
  1803. int rc=1;
  1804. u8 event = le32_to_cpu(reply->Event) & 0xFF;
  1805. if (!ioc->sh)
  1806. goto out;
  1807. /*
  1808. * sas_discovery_ignore_events
  1809. *
  1810. * This flag is to prevent anymore processing of
  1811. * sas events once mptsas_remove function is called.
  1812. */
  1813. if (ioc->sas_discovery_ignore_events) {
  1814. rc = mptscsih_event_process(ioc, reply);
  1815. goto out;
  1816. }
  1817. switch (event) {
  1818. case MPI_EVENT_SAS_DEVICE_STATUS_CHANGE:
  1819. mptscsih_send_sas_event(ioc,
  1820. (EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *)reply->Data);
  1821. break;
  1822. case MPI_EVENT_INTEGRATED_RAID:
  1823. mptscsih_send_raid_event(ioc,
  1824. (EVENT_DATA_RAID *)reply->Data);
  1825. break;
  1826. case MPI_EVENT_PERSISTENT_TABLE_FULL:
  1827. INIT_WORK(&ioc->mptscsih_persistTask,
  1828. mptscsih_sas_persist_clear_table,
  1829. (void *)ioc);
  1830. schedule_work(&ioc->mptscsih_persistTask);
  1831. break;
  1832. case MPI_EVENT_SAS_DISCOVERY:
  1833. mptscsih_send_discovery(ioc,
  1834. (EVENT_DATA_SAS_DISCOVERY *)reply->Data);
  1835. break;
  1836. default:
  1837. rc = mptscsih_event_process(ioc, reply);
  1838. break;
  1839. }
  1840. out:
  1841. return rc;
  1842. }
  1843. static int
  1844. mptsas_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  1845. {
  1846. struct Scsi_Host *sh;
  1847. MPT_SCSI_HOST *hd;
  1848. MPT_ADAPTER *ioc;
  1849. unsigned long flags;
  1850. int ii;
  1851. int numSGE = 0;
  1852. int scale;
  1853. int ioc_cap;
  1854. int error=0;
  1855. int r;
  1856. r = mpt_attach(pdev,id);
  1857. if (r)
  1858. return r;
  1859. ioc = pci_get_drvdata(pdev);
  1860. ioc->DoneCtx = mptsasDoneCtx;
  1861. ioc->TaskCtx = mptsasTaskCtx;
  1862. ioc->InternalCtx = mptsasInternalCtx;
  1863. /* Added sanity check on readiness of the MPT adapter.
  1864. */
  1865. if (ioc->last_state != MPI_IOC_STATE_OPERATIONAL) {
  1866. printk(MYIOC_s_WARN_FMT
  1867. "Skipping because it's not operational!\n",
  1868. ioc->name);
  1869. error = -ENODEV;
  1870. goto out_mptsas_probe;
  1871. }
  1872. if (!ioc->active) {
  1873. printk(MYIOC_s_WARN_FMT "Skipping because it's disabled!\n",
  1874. ioc->name);
  1875. error = -ENODEV;
  1876. goto out_mptsas_probe;
  1877. }
  1878. /* Sanity check - ensure at least 1 port is INITIATOR capable
  1879. */
  1880. ioc_cap = 0;
  1881. for (ii = 0; ii < ioc->facts.NumberOfPorts; ii++) {
  1882. if (ioc->pfacts[ii].ProtocolFlags &
  1883. MPI_PORTFACTS_PROTOCOL_INITIATOR)
  1884. ioc_cap++;
  1885. }
  1886. if (!ioc_cap) {
  1887. printk(MYIOC_s_WARN_FMT
  1888. "Skipping ioc=%p because SCSI Initiator mode "
  1889. "is NOT enabled!\n", ioc->name, ioc);
  1890. return 0;
  1891. }
  1892. sh = scsi_host_alloc(&mptsas_driver_template, sizeof(MPT_SCSI_HOST));
  1893. if (!sh) {
  1894. printk(MYIOC_s_WARN_FMT
  1895. "Unable to register controller with SCSI subsystem\n",
  1896. ioc->name);
  1897. error = -1;
  1898. goto out_mptsas_probe;
  1899. }
  1900. spin_lock_irqsave(&ioc->FreeQlock, flags);
  1901. /* Attach the SCSI Host to the IOC structure
  1902. */
  1903. ioc->sh = sh;
  1904. sh->io_port = 0;
  1905. sh->n_io_port = 0;
  1906. sh->irq = 0;
  1907. /* set 16 byte cdb's */
  1908. sh->max_cmd_len = 16;
  1909. sh->max_id = ioc->pfacts->MaxDevices + 1;
  1910. sh->transportt = mptsas_transport_template;
  1911. sh->max_lun = MPT_LAST_LUN + 1;
  1912. sh->max_channel = 0;
  1913. sh->this_id = ioc->pfacts[0].PortSCSIID;
  1914. /* Required entry.
  1915. */
  1916. sh->unique_id = ioc->id;
  1917. INIT_LIST_HEAD(&ioc->sas_topology);
  1918. mutex_init(&ioc->sas_topology_mutex);
  1919. mutex_init(&ioc->sas_discovery_mutex);
  1920. mutex_init(&ioc->sas_mgmt.mutex);
  1921. init_completion(&ioc->sas_mgmt.done);
  1922. /* Verify that we won't exceed the maximum
  1923. * number of chain buffers
  1924. * We can optimize: ZZ = req_sz/sizeof(SGE)
  1925. * For 32bit SGE's:
  1926. * numSGE = 1 + (ZZ-1)*(maxChain -1) + ZZ
  1927. * + (req_sz - 64)/sizeof(SGE)
  1928. * A slightly different algorithm is required for
  1929. * 64bit SGEs.
  1930. */
  1931. scale = ioc->req_sz/(sizeof(dma_addr_t) + sizeof(u32));
  1932. if (sizeof(dma_addr_t) == sizeof(u64)) {
  1933. numSGE = (scale - 1) *
  1934. (ioc->facts.MaxChainDepth-1) + scale +
  1935. (ioc->req_sz - 60) / (sizeof(dma_addr_t) +
  1936. sizeof(u32));
  1937. } else {
  1938. numSGE = 1 + (scale - 1) *
  1939. (ioc->facts.MaxChainDepth-1) + scale +
  1940. (ioc->req_sz - 64) / (sizeof(dma_addr_t) +
  1941. sizeof(u32));
  1942. }
  1943. if (numSGE < sh->sg_tablesize) {
  1944. /* Reset this value */
  1945. dprintk((MYIOC_s_INFO_FMT
  1946. "Resetting sg_tablesize to %d from %d\n",
  1947. ioc->name, numSGE, sh->sg_tablesize));
  1948. sh->sg_tablesize = numSGE;
  1949. }
  1950. spin_unlock_irqrestore(&ioc->FreeQlock, flags);
  1951. hd = (MPT_SCSI_HOST *) sh->hostdata;
  1952. hd->ioc = ioc;
  1953. /* SCSI needs scsi_cmnd lookup table!
  1954. * (with size equal to req_depth*PtrSz!)
  1955. */
  1956. hd->ScsiLookup = kcalloc(ioc->req_depth, sizeof(void *), GFP_ATOMIC);
  1957. if (!hd->ScsiLookup) {
  1958. error = -ENOMEM;
  1959. goto out_mptsas_probe;
  1960. }
  1961. dprintk((MYIOC_s_INFO_FMT "ScsiLookup @ %p\n",
  1962. ioc->name, hd->ScsiLookup));
  1963. /* Allocate memory for the device structures.
  1964. * A non-Null pointer at an offset
  1965. * indicates a device exists.
  1966. * max_id = 1 + maximum id (hosts.h)
  1967. */
  1968. hd->Targets = kcalloc(sh->max_id, sizeof(void *), GFP_ATOMIC);
  1969. if (!hd->Targets) {
  1970. error = -ENOMEM;
  1971. goto out_mptsas_probe;
  1972. }
  1973. dprintk((KERN_INFO " vtarget @ %p\n", hd->Targets));
  1974. /* Clear the TM flags
  1975. */
  1976. hd->tmPending = 0;
  1977. hd->tmState = TM_STATE_NONE;
  1978. hd->resetPending = 0;
  1979. hd->abortSCpnt = NULL;
  1980. /* Clear the pointer used to store
  1981. * single-threaded commands, i.e., those
  1982. * issued during a bus scan, dv and
  1983. * configuration pages.
  1984. */
  1985. hd->cmdPtr = NULL;
  1986. /* Initialize this SCSI Hosts' timers
  1987. * To use, set the timer expires field
  1988. * and add_timer
  1989. */
  1990. init_timer(&hd->timer);
  1991. hd->timer.data = (unsigned long) hd;
  1992. hd->timer.function = mptscsih_timer_expired;
  1993. hd->mpt_pq_filter = mpt_pq_filter;
  1994. ioc->sas_data.ptClear = mpt_pt_clear;
  1995. if (ioc->sas_data.ptClear==1) {
  1996. mptbase_sas_persist_operation(
  1997. ioc, MPI_SAS_OP_CLEAR_ALL_PERSISTENT);
  1998. }
  1999. ddvprintk((MYIOC_s_INFO_FMT
  2000. "mpt_pq_filter %x mpt_pq_filter %x\n",
  2001. ioc->name,
  2002. mpt_pq_filter,
  2003. mpt_pq_filter));
  2004. init_waitqueue_head(&hd->scandv_waitq);
  2005. hd->scandv_wait_done = 0;
  2006. hd->last_queue_full = 0;
  2007. error = scsi_add_host(sh, &ioc->pcidev->dev);
  2008. if (error) {
  2009. dprintk((KERN_ERR MYNAM
  2010. "scsi_add_host failed\n"));
  2011. goto out_mptsas_probe;
  2012. }
  2013. mptsas_scan_sas_topology(ioc);
  2014. return 0;
  2015. out_mptsas_probe:
  2016. mptscsih_remove(pdev);
  2017. return error;
  2018. }
  2019. static void __devexit mptsas_remove(struct pci_dev *pdev)
  2020. {
  2021. MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
  2022. struct mptsas_portinfo *p, *n;
  2023. ioc->sas_discovery_ignore_events=1;
  2024. sas_remove_host(ioc->sh);
  2025. mutex_lock(&ioc->sas_topology_mutex);
  2026. list_for_each_entry_safe(p, n, &ioc->sas_topology, list) {
  2027. list_del(&p->list);
  2028. if (p->phy_info)
  2029. kfree(p->phy_info);
  2030. kfree(p);
  2031. }
  2032. mutex_unlock(&ioc->sas_topology_mutex);
  2033. mptscsih_remove(pdev);
  2034. }
  2035. static struct pci_device_id mptsas_pci_table[] = {
  2036. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064,
  2037. PCI_ANY_ID, PCI_ANY_ID },
  2038. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1066,
  2039. PCI_ANY_ID, PCI_ANY_ID },
  2040. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1068,
  2041. PCI_ANY_ID, PCI_ANY_ID },
  2042. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064E,
  2043. PCI_ANY_ID, PCI_ANY_ID },
  2044. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1066E,
  2045. PCI_ANY_ID, PCI_ANY_ID },
  2046. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1068E,
  2047. PCI_ANY_ID, PCI_ANY_ID },
  2048. {0} /* Terminating entry */
  2049. };
  2050. MODULE_DEVICE_TABLE(pci, mptsas_pci_table);
  2051. static struct pci_driver mptsas_driver = {
  2052. .name = "mptsas",
  2053. .id_table = mptsas_pci_table,
  2054. .probe = mptsas_probe,
  2055. .remove = __devexit_p(mptsas_remove),
  2056. .shutdown = mptscsih_shutdown,
  2057. #ifdef CONFIG_PM
  2058. .suspend = mptscsih_suspend,
  2059. .resume = mptscsih_resume,
  2060. #endif
  2061. };
  2062. static int __init
  2063. mptsas_init(void)
  2064. {
  2065. show_mptmod_ver(my_NAME, my_VERSION);
  2066. mptsas_transport_template =
  2067. sas_attach_transport(&mptsas_transport_functions);
  2068. if (!mptsas_transport_template)
  2069. return -ENODEV;
  2070. mptsasDoneCtx = mpt_register(mptscsih_io_done, MPTSAS_DRIVER);
  2071. mptsasTaskCtx = mpt_register(mptscsih_taskmgmt_complete, MPTSAS_DRIVER);
  2072. mptsasInternalCtx =
  2073. mpt_register(mptscsih_scandv_complete, MPTSAS_DRIVER);
  2074. mptsasMgmtCtx = mpt_register(mptsas_mgmt_done, MPTSAS_DRIVER);
  2075. if (mpt_event_register(mptsasDoneCtx, mptsas_event_process) == 0) {
  2076. devtverboseprintk((KERN_INFO MYNAM
  2077. ": Registered for IOC event notifications\n"));
  2078. }
  2079. if (mpt_reset_register(mptsasDoneCtx, mptscsih_ioc_reset) == 0) {
  2080. dprintk((KERN_INFO MYNAM
  2081. ": Registered for IOC reset notifications\n"));
  2082. }
  2083. return pci_register_driver(&mptsas_driver);
  2084. }
  2085. static void __exit
  2086. mptsas_exit(void)
  2087. {
  2088. pci_unregister_driver(&mptsas_driver);
  2089. sas_release_transport(mptsas_transport_template);
  2090. mpt_reset_deregister(mptsasDoneCtx);
  2091. mpt_event_deregister(mptsasDoneCtx);
  2092. mpt_deregister(mptsasMgmtCtx);
  2093. mpt_deregister(mptsasInternalCtx);
  2094. mpt_deregister(mptsasTaskCtx);
  2095. mpt_deregister(mptsasDoneCtx);
  2096. }
  2097. module_init(mptsas_init);
  2098. module_exit(mptsas_exit);