esas2r_main.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985
  1. /*
  2. * linux/drivers/scsi/esas2r/esas2r_main.c
  3. * For use with ATTO ExpressSAS R6xx SAS/SATA RAID controllers
  4. *
  5. * Copyright (c) 2001-2013 ATTO Technology, Inc.
  6. * (mailto:linuxdrivers@attotech.com)
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * NO WARRANTY
  19. * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
  20. * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
  21. * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
  22. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
  23. * solely responsible for determining the appropriateness of using and
  24. * distributing the Program and assumes all risks associated with its
  25. * exercise of rights under this Agreement, including but not limited to
  26. * the risks and costs of program errors, damage to or loss of data,
  27. * programs or equipment, and unavailability or interruption of operations.
  28. *
  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. *
  38. * You should have received a copy of the GNU General Public License
  39. * along with this program; if not, write to the Free Software
  40. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  41. * USA.
  42. */
  43. #include "esas2r.h"
  44. MODULE_DESCRIPTION(ESAS2R_DRVR_NAME ": " ESAS2R_LONGNAME " driver");
  45. MODULE_AUTHOR("ATTO Technology, Inc.");
  46. MODULE_LICENSE("GPL");
  47. MODULE_VERSION(ESAS2R_VERSION_STR);
  48. /* global definitions */
  49. static int found_adapters;
  50. struct esas2r_adapter *esas2r_adapters[MAX_ADAPTERS];
  51. #define ESAS2R_VDA_EVENT_PORT1 54414
  52. #define ESAS2R_VDA_EVENT_PORT2 54415
  53. #define ESAS2R_VDA_EVENT_SOCK_COUNT 2
  54. static struct esas2r_adapter *esas2r_adapter_from_kobj(struct kobject *kobj)
  55. {
  56. struct device *dev = container_of(kobj, struct device, kobj);
  57. struct Scsi_Host *host = class_to_shost(dev);
  58. return (struct esas2r_adapter *)host->hostdata;
  59. }
  60. static ssize_t read_fw(struct file *file, struct kobject *kobj,
  61. struct bin_attribute *attr,
  62. char *buf, loff_t off, size_t count)
  63. {
  64. struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
  65. return esas2r_read_fw(a, buf, off, count);
  66. }
  67. static ssize_t write_fw(struct file *file, struct kobject *kobj,
  68. struct bin_attribute *attr,
  69. char *buf, loff_t off, size_t count)
  70. {
  71. struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
  72. return esas2r_write_fw(a, buf, off, count);
  73. }
  74. static ssize_t read_fs(struct file *file, struct kobject *kobj,
  75. struct bin_attribute *attr,
  76. char *buf, loff_t off, size_t count)
  77. {
  78. struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
  79. return esas2r_read_fs(a, buf, off, count);
  80. }
  81. static ssize_t write_fs(struct file *file, struct kobject *kobj,
  82. struct bin_attribute *attr,
  83. char *buf, loff_t off, size_t count)
  84. {
  85. struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
  86. int length = min(sizeof(struct esas2r_ioctl_fs), count);
  87. int result = 0;
  88. result = esas2r_write_fs(a, buf, off, count);
  89. if (result < 0)
  90. result = 0;
  91. return length;
  92. }
  93. static ssize_t read_vda(struct file *file, struct kobject *kobj,
  94. struct bin_attribute *attr,
  95. char *buf, loff_t off, size_t count)
  96. {
  97. struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
  98. return esas2r_read_vda(a, buf, off, count);
  99. }
  100. static ssize_t write_vda(struct file *file, struct kobject *kobj,
  101. struct bin_attribute *attr,
  102. char *buf, loff_t off, size_t count)
  103. {
  104. struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
  105. return esas2r_write_vda(a, buf, off, count);
  106. }
  107. static ssize_t read_live_nvram(struct file *file, struct kobject *kobj,
  108. struct bin_attribute *attr,
  109. char *buf, loff_t off, size_t count)
  110. {
  111. struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
  112. int length = min_t(size_t, sizeof(struct esas2r_sas_nvram), PAGE_SIZE);
  113. memcpy(buf, a->nvram, length);
  114. return length;
  115. }
  116. static ssize_t write_live_nvram(struct file *file, struct kobject *kobj,
  117. struct bin_attribute *attr,
  118. char *buf, loff_t off, size_t count)
  119. {
  120. struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
  121. struct esas2r_request *rq;
  122. int result = -EFAULT;
  123. rq = esas2r_alloc_request(a);
  124. if (rq == NULL)
  125. return -ENOMEM;
  126. if (esas2r_write_params(a, rq, (struct esas2r_sas_nvram *)buf))
  127. result = count;
  128. esas2r_free_request(a, rq);
  129. return result;
  130. }
  131. static ssize_t read_default_nvram(struct file *file, struct kobject *kobj,
  132. struct bin_attribute *attr,
  133. char *buf, loff_t off, size_t count)
  134. {
  135. struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
  136. esas2r_nvram_get_defaults(a, (struct esas2r_sas_nvram *)buf);
  137. return sizeof(struct esas2r_sas_nvram);
  138. }
  139. static ssize_t read_hw(struct file *file, struct kobject *kobj,
  140. struct bin_attribute *attr,
  141. char *buf, loff_t off, size_t count)
  142. {
  143. struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
  144. int length = min_t(size_t, sizeof(struct atto_ioctl), PAGE_SIZE);
  145. if (!a->local_atto_ioctl)
  146. return -ENOMEM;
  147. if (handle_hba_ioctl(a, a->local_atto_ioctl) != IOCTL_SUCCESS)
  148. return -ENOMEM;
  149. memcpy(buf, a->local_atto_ioctl, length);
  150. return length;
  151. }
  152. static ssize_t write_hw(struct file *file, struct kobject *kobj,
  153. struct bin_attribute *attr,
  154. char *buf, loff_t off, size_t count)
  155. {
  156. struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
  157. int length = min(sizeof(struct atto_ioctl), count);
  158. if (!a->local_atto_ioctl) {
  159. a->local_atto_ioctl = kzalloc(sizeof(struct atto_ioctl),
  160. GFP_KERNEL);
  161. if (a->local_atto_ioctl == NULL) {
  162. esas2r_log(ESAS2R_LOG_WARN,
  163. "write_hw kzalloc failed for %d bytes",
  164. sizeof(struct atto_ioctl));
  165. return -ENOMEM;
  166. }
  167. }
  168. memset(a->local_atto_ioctl, 0, sizeof(struct atto_ioctl));
  169. memcpy(a->local_atto_ioctl, buf, length);
  170. return length;
  171. }
  172. #define ESAS2R_RW_BIN_ATTR(_name) \
  173. struct bin_attribute bin_attr_ ## _name = { \
  174. .attr = \
  175. { .name = __stringify(_name), .mode = S_IRUSR | S_IWUSR }, \
  176. .size = 0, \
  177. .read = read_ ## _name, \
  178. .write = write_ ## _name }
  179. ESAS2R_RW_BIN_ATTR(fw);
  180. ESAS2R_RW_BIN_ATTR(fs);
  181. ESAS2R_RW_BIN_ATTR(vda);
  182. ESAS2R_RW_BIN_ATTR(hw);
  183. ESAS2R_RW_BIN_ATTR(live_nvram);
  184. struct bin_attribute bin_attr_default_nvram = {
  185. .attr = { .name = "default_nvram", .mode = S_IRUGO },
  186. .size = 0,
  187. .read = read_default_nvram,
  188. .write = NULL
  189. };
  190. static struct scsi_host_template driver_template = {
  191. .module = THIS_MODULE,
  192. .show_info = esas2r_show_info,
  193. .name = ESAS2R_LONGNAME,
  194. .release = esas2r_release,
  195. .info = esas2r_info,
  196. .ioctl = esas2r_ioctl,
  197. .queuecommand = esas2r_queuecommand,
  198. .eh_abort_handler = esas2r_eh_abort,
  199. .eh_device_reset_handler = esas2r_device_reset,
  200. .eh_bus_reset_handler = esas2r_bus_reset,
  201. .eh_host_reset_handler = esas2r_host_reset,
  202. .eh_target_reset_handler = esas2r_target_reset,
  203. .can_queue = 128,
  204. .this_id = -1,
  205. .sg_tablesize = SCSI_MAX_SG_SEGMENTS,
  206. .cmd_per_lun =
  207. ESAS2R_DEFAULT_CMD_PER_LUN,
  208. .present = 0,
  209. .unchecked_isa_dma = 0,
  210. .use_clustering = ENABLE_CLUSTERING,
  211. .emulated = 0,
  212. .proc_name = ESAS2R_DRVR_NAME,
  213. .change_queue_depth = esas2r_change_queue_depth,
  214. .change_queue_type = scsi_change_queue_type,
  215. .max_sectors = 0xFFFF,
  216. .use_blk_tags = 1,
  217. };
  218. int sgl_page_size = 512;
  219. module_param(sgl_page_size, int, 0);
  220. MODULE_PARM_DESC(sgl_page_size,
  221. "Scatter/gather list (SGL) page size in number of S/G "
  222. "entries. If your application is doing a lot of very large "
  223. "transfers, you may want to increase the SGL page size. "
  224. "Default 512.");
  225. int num_sg_lists = 1024;
  226. module_param(num_sg_lists, int, 0);
  227. MODULE_PARM_DESC(num_sg_lists,
  228. "Number of scatter/gather lists. Default 1024.");
  229. int sg_tablesize = SCSI_MAX_SG_SEGMENTS;
  230. module_param(sg_tablesize, int, 0);
  231. MODULE_PARM_DESC(sg_tablesize,
  232. "Maximum number of entries in a scatter/gather table.");
  233. int num_requests = 256;
  234. module_param(num_requests, int, 0);
  235. MODULE_PARM_DESC(num_requests,
  236. "Number of requests. Default 256.");
  237. int num_ae_requests = 4;
  238. module_param(num_ae_requests, int, 0);
  239. MODULE_PARM_DESC(num_ae_requests,
  240. "Number of VDA asynchromous event requests. Default 4.");
  241. int cmd_per_lun = ESAS2R_DEFAULT_CMD_PER_LUN;
  242. module_param(cmd_per_lun, int, 0);
  243. MODULE_PARM_DESC(cmd_per_lun,
  244. "Maximum number of commands per LUN. Default "
  245. DEFINED_NUM_TO_STR(ESAS2R_DEFAULT_CMD_PER_LUN) ".");
  246. int can_queue = 128;
  247. module_param(can_queue, int, 0);
  248. MODULE_PARM_DESC(can_queue,
  249. "Maximum number of commands per adapter. Default 128.");
  250. int esas2r_max_sectors = 0xFFFF;
  251. module_param(esas2r_max_sectors, int, 0);
  252. MODULE_PARM_DESC(esas2r_max_sectors,
  253. "Maximum number of disk sectors in a single data transfer. "
  254. "Default 65535 (largest possible setting).");
  255. int interrupt_mode = 1;
  256. module_param(interrupt_mode, int, 0);
  257. MODULE_PARM_DESC(interrupt_mode,
  258. "Defines the interrupt mode to use. 0 for legacy"
  259. ", 1 for MSI. Default is MSI (1).");
  260. static struct pci_device_id
  261. esas2r_pci_table[] = {
  262. { ATTO_VENDOR_ID, 0x0049, ATTO_VENDOR_ID, 0x0049,
  263. 0,
  264. 0, 0 },
  265. { ATTO_VENDOR_ID, 0x0049, ATTO_VENDOR_ID, 0x004A,
  266. 0,
  267. 0, 0 },
  268. { ATTO_VENDOR_ID, 0x0049, ATTO_VENDOR_ID, 0x004B,
  269. 0,
  270. 0, 0 },
  271. { ATTO_VENDOR_ID, 0x0049, ATTO_VENDOR_ID, 0x004C,
  272. 0,
  273. 0, 0 },
  274. { ATTO_VENDOR_ID, 0x0049, ATTO_VENDOR_ID, 0x004D,
  275. 0,
  276. 0, 0 },
  277. { ATTO_VENDOR_ID, 0x0049, ATTO_VENDOR_ID, 0x004E,
  278. 0,
  279. 0, 0 },
  280. { 0, 0, 0, 0,
  281. 0,
  282. 0, 0 }
  283. };
  284. MODULE_DEVICE_TABLE(pci, esas2r_pci_table);
  285. static int
  286. esas2r_probe(struct pci_dev *pcid, const struct pci_device_id *id);
  287. static void
  288. esas2r_remove(struct pci_dev *pcid);
  289. static struct pci_driver
  290. esas2r_pci_driver = {
  291. .name = ESAS2R_DRVR_NAME,
  292. .id_table = esas2r_pci_table,
  293. .probe = esas2r_probe,
  294. .remove = esas2r_remove,
  295. .suspend = esas2r_suspend,
  296. .resume = esas2r_resume,
  297. };
  298. static int esas2r_probe(struct pci_dev *pcid,
  299. const struct pci_device_id *id)
  300. {
  301. struct Scsi_Host *host = NULL;
  302. struct esas2r_adapter *a;
  303. int err;
  304. size_t host_alloc_size = sizeof(struct esas2r_adapter)
  305. + ((num_requests) +
  306. 1) * sizeof(struct esas2r_request);
  307. esas2r_log_dev(ESAS2R_LOG_DEBG, &(pcid->dev),
  308. "esas2r_probe() 0x%02x 0x%02x 0x%02x 0x%02x",
  309. pcid->vendor,
  310. pcid->device,
  311. pcid->subsystem_vendor,
  312. pcid->subsystem_device);
  313. esas2r_log_dev(ESAS2R_LOG_INFO, &(pcid->dev),
  314. "before pci_enable_device() "
  315. "enable_cnt: %d",
  316. pcid->enable_cnt.counter);
  317. err = pci_enable_device(pcid);
  318. if (err != 0) {
  319. esas2r_log_dev(ESAS2R_LOG_CRIT, &(pcid->dev),
  320. "pci_enable_device() FAIL (%d)",
  321. err);
  322. return -ENODEV;
  323. }
  324. esas2r_log_dev(ESAS2R_LOG_INFO, &(pcid->dev),
  325. "pci_enable_device() OK");
  326. esas2r_log_dev(ESAS2R_LOG_INFO, &(pcid->dev),
  327. "after pci_enable_device() enable_cnt: %d",
  328. pcid->enable_cnt.counter);
  329. host = scsi_host_alloc(&driver_template, host_alloc_size);
  330. if (host == NULL) {
  331. esas2r_log(ESAS2R_LOG_CRIT, "scsi_host_alloc() FAIL");
  332. return -ENODEV;
  333. }
  334. memset(host->hostdata, 0, host_alloc_size);
  335. a = (struct esas2r_adapter *)host->hostdata;
  336. esas2r_log(ESAS2R_LOG_INFO, "scsi_host_alloc() OK host: %p", host);
  337. /* override max LUN and max target id */
  338. host->max_id = ESAS2R_MAX_ID + 1;
  339. host->max_lun = 255;
  340. /* we can handle 16-byte CDbs */
  341. host->max_cmd_len = 16;
  342. host->can_queue = can_queue;
  343. host->cmd_per_lun = cmd_per_lun;
  344. host->this_id = host->max_id + 1;
  345. host->max_channel = 0;
  346. host->unique_id = found_adapters;
  347. host->sg_tablesize = sg_tablesize;
  348. host->max_sectors = esas2r_max_sectors;
  349. /* set to bus master for BIOses that don't do it for us */
  350. esas2r_log(ESAS2R_LOG_INFO, "pci_set_master() called");
  351. pci_set_master(pcid);
  352. if (!esas2r_init_adapter(host, pcid, found_adapters)) {
  353. esas2r_log(ESAS2R_LOG_CRIT,
  354. "unable to initialize device at PCI bus %x:%x",
  355. pcid->bus->number,
  356. pcid->devfn);
  357. esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev),
  358. "scsi_host_put() called");
  359. scsi_host_put(host);
  360. return 0;
  361. }
  362. esas2r_log(ESAS2R_LOG_INFO, "pci_set_drvdata(%p, %p) called", pcid,
  363. host->hostdata);
  364. pci_set_drvdata(pcid, host);
  365. esas2r_log(ESAS2R_LOG_INFO, "scsi_add_host() called");
  366. err = scsi_add_host(host, &pcid->dev);
  367. if (err) {
  368. esas2r_log(ESAS2R_LOG_CRIT, "scsi_add_host returned %d", err);
  369. esas2r_log_dev(ESAS2R_LOG_CRIT, &(host->shost_gendev),
  370. "scsi_add_host() FAIL");
  371. esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev),
  372. "scsi_host_put() called");
  373. scsi_host_put(host);
  374. esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev),
  375. "pci_set_drvdata(%p, NULL) called",
  376. pcid);
  377. pci_set_drvdata(pcid, NULL);
  378. return -ENODEV;
  379. }
  380. esas2r_fw_event_on(a);
  381. esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev),
  382. "scsi_scan_host() called");
  383. scsi_scan_host(host);
  384. /* Add sysfs binary files */
  385. if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_fw))
  386. esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
  387. "Failed to create sysfs binary file: fw");
  388. else
  389. a->sysfs_fw_created = 1;
  390. if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_fs))
  391. esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
  392. "Failed to create sysfs binary file: fs");
  393. else
  394. a->sysfs_fs_created = 1;
  395. if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_vda))
  396. esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
  397. "Failed to create sysfs binary file: vda");
  398. else
  399. a->sysfs_vda_created = 1;
  400. if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_hw))
  401. esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
  402. "Failed to create sysfs binary file: hw");
  403. else
  404. a->sysfs_hw_created = 1;
  405. if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_live_nvram))
  406. esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
  407. "Failed to create sysfs binary file: live_nvram");
  408. else
  409. a->sysfs_live_nvram_created = 1;
  410. if (sysfs_create_bin_file(&host->shost_dev.kobj,
  411. &bin_attr_default_nvram))
  412. esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
  413. "Failed to create sysfs binary file: default_nvram");
  414. else
  415. a->sysfs_default_nvram_created = 1;
  416. found_adapters++;
  417. return 0;
  418. }
  419. static void esas2r_remove(struct pci_dev *pdev)
  420. {
  421. struct Scsi_Host *host;
  422. int index;
  423. if (pdev == NULL) {
  424. esas2r_log(ESAS2R_LOG_WARN, "esas2r_remove pdev==NULL");
  425. return;
  426. }
  427. host = pci_get_drvdata(pdev);
  428. if (host == NULL) {
  429. /*
  430. * this can happen if pci_set_drvdata was already called
  431. * to clear the host pointer. if this is the case, we
  432. * are okay; this channel has already been cleaned up.
  433. */
  434. return;
  435. }
  436. esas2r_log_dev(ESAS2R_LOG_INFO, &(pdev->dev),
  437. "esas2r_remove(%p) called; "
  438. "host:%p", pdev,
  439. host);
  440. index = esas2r_cleanup(host);
  441. if (index < 0)
  442. esas2r_log_dev(ESAS2R_LOG_WARN, &(pdev->dev),
  443. "unknown host in %s",
  444. __func__);
  445. found_adapters--;
  446. /* if this was the last adapter, clean up the rest of the driver */
  447. if (found_adapters == 0)
  448. esas2r_cleanup(NULL);
  449. }
  450. static int __init esas2r_init(void)
  451. {
  452. int i;
  453. esas2r_log(ESAS2R_LOG_INFO, "%s called", __func__);
  454. /* verify valid parameters */
  455. if (can_queue < 1) {
  456. esas2r_log(ESAS2R_LOG_WARN,
  457. "warning: can_queue must be at least 1, value "
  458. "forced.");
  459. can_queue = 1;
  460. } else if (can_queue > 2048) {
  461. esas2r_log(ESAS2R_LOG_WARN,
  462. "warning: can_queue must be no larger than 2048, "
  463. "value forced.");
  464. can_queue = 2048;
  465. }
  466. if (cmd_per_lun < 1) {
  467. esas2r_log(ESAS2R_LOG_WARN,
  468. "warning: cmd_per_lun must be at least 1, value "
  469. "forced.");
  470. cmd_per_lun = 1;
  471. } else if (cmd_per_lun > 2048) {
  472. esas2r_log(ESAS2R_LOG_WARN,
  473. "warning: cmd_per_lun must be no larger than "
  474. "2048, value forced.");
  475. cmd_per_lun = 2048;
  476. }
  477. if (sg_tablesize < 32) {
  478. esas2r_log(ESAS2R_LOG_WARN,
  479. "warning: sg_tablesize must be at least 32, "
  480. "value forced.");
  481. sg_tablesize = 32;
  482. }
  483. if (esas2r_max_sectors < 1) {
  484. esas2r_log(ESAS2R_LOG_WARN,
  485. "warning: esas2r_max_sectors must be at least "
  486. "1, value forced.");
  487. esas2r_max_sectors = 1;
  488. } else if (esas2r_max_sectors > 0xffff) {
  489. esas2r_log(ESAS2R_LOG_WARN,
  490. "warning: esas2r_max_sectors must be no larger "
  491. "than 0xffff, value forced.");
  492. esas2r_max_sectors = 0xffff;
  493. }
  494. sgl_page_size &= ~(ESAS2R_SGL_ALIGN - 1);
  495. if (sgl_page_size < SGL_PG_SZ_MIN)
  496. sgl_page_size = SGL_PG_SZ_MIN;
  497. else if (sgl_page_size > SGL_PG_SZ_MAX)
  498. sgl_page_size = SGL_PG_SZ_MAX;
  499. if (num_sg_lists < NUM_SGL_MIN)
  500. num_sg_lists = NUM_SGL_MIN;
  501. else if (num_sg_lists > NUM_SGL_MAX)
  502. num_sg_lists = NUM_SGL_MAX;
  503. if (num_requests < NUM_REQ_MIN)
  504. num_requests = NUM_REQ_MIN;
  505. else if (num_requests > NUM_REQ_MAX)
  506. num_requests = NUM_REQ_MAX;
  507. if (num_ae_requests < NUM_AE_MIN)
  508. num_ae_requests = NUM_AE_MIN;
  509. else if (num_ae_requests > NUM_AE_MAX)
  510. num_ae_requests = NUM_AE_MAX;
  511. /* set up other globals */
  512. for (i = 0; i < MAX_ADAPTERS; i++)
  513. esas2r_adapters[i] = NULL;
  514. /* initialize */
  515. driver_template.module = THIS_MODULE;
  516. if (pci_register_driver(&esas2r_pci_driver) != 0)
  517. esas2r_log(ESAS2R_LOG_CRIT, "pci_register_driver FAILED");
  518. else
  519. esas2r_log(ESAS2R_LOG_INFO, "pci_register_driver() OK");
  520. if (!found_adapters) {
  521. pci_unregister_driver(&esas2r_pci_driver);
  522. esas2r_cleanup(NULL);
  523. esas2r_log(ESAS2R_LOG_CRIT,
  524. "driver will not be loaded because no ATTO "
  525. "%s devices were found",
  526. ESAS2R_DRVR_NAME);
  527. return -1;
  528. } else {
  529. esas2r_log(ESAS2R_LOG_INFO, "found %d adapters",
  530. found_adapters);
  531. }
  532. return 0;
  533. }
  534. /* Handle ioctl calls to "/proc/scsi/esas2r/ATTOnode" */
  535. static const struct file_operations esas2r_proc_fops = {
  536. .compat_ioctl = esas2r_proc_ioctl,
  537. .unlocked_ioctl = esas2r_proc_ioctl,
  538. };
  539. static struct Scsi_Host *esas2r_proc_host;
  540. static int esas2r_proc_major;
  541. long esas2r_proc_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
  542. {
  543. return esas2r_ioctl_handler(esas2r_proc_host->hostdata,
  544. (int)cmd, (void __user *)arg);
  545. }
  546. static void __exit esas2r_exit(void)
  547. {
  548. esas2r_log(ESAS2R_LOG_INFO, "%s called", __func__);
  549. if (esas2r_proc_major > 0) {
  550. esas2r_log(ESAS2R_LOG_INFO, "unregister proc");
  551. remove_proc_entry(ATTONODE_NAME,
  552. esas2r_proc_host->hostt->proc_dir);
  553. unregister_chrdev(esas2r_proc_major, ESAS2R_DRVR_NAME);
  554. esas2r_proc_major = 0;
  555. }
  556. esas2r_log(ESAS2R_LOG_INFO, "pci_unregister_driver() called");
  557. pci_unregister_driver(&esas2r_pci_driver);
  558. }
  559. int esas2r_show_info(struct seq_file *m, struct Scsi_Host *sh)
  560. {
  561. struct esas2r_adapter *a = (struct esas2r_adapter *)sh->hostdata;
  562. struct esas2r_target *t;
  563. int dev_count = 0;
  564. esas2r_log(ESAS2R_LOG_DEBG, "esas2r_show_info (%p,%d)", m, sh->host_no);
  565. seq_printf(m, ESAS2R_LONGNAME "\n"
  566. "Driver version: "ESAS2R_VERSION_STR "\n"
  567. "Flash version: %s\n"
  568. "Firmware version: %s\n"
  569. "Copyright "ESAS2R_COPYRIGHT_YEARS "\n"
  570. "http://www.attotech.com\n"
  571. "\n",
  572. a->flash_rev,
  573. a->fw_rev[0] ? a->fw_rev : "(none)");
  574. seq_printf(m, "Adapter information:\n"
  575. "--------------------\n"
  576. "Model: %s\n"
  577. "SAS address: %02X%02X%02X%02X:%02X%02X%02X%02X\n",
  578. esas2r_get_model_name(a),
  579. a->nvram->sas_addr[0],
  580. a->nvram->sas_addr[1],
  581. a->nvram->sas_addr[2],
  582. a->nvram->sas_addr[3],
  583. a->nvram->sas_addr[4],
  584. a->nvram->sas_addr[5],
  585. a->nvram->sas_addr[6],
  586. a->nvram->sas_addr[7]);
  587. seq_puts(m, "\n"
  588. "Discovered devices:\n"
  589. "\n"
  590. " # Target ID\n"
  591. "---------------\n");
  592. for (t = a->targetdb; t < a->targetdb_end; t++)
  593. if (t->buffered_target_state == TS_PRESENT) {
  594. seq_printf(m, " %3d %3d\n",
  595. ++dev_count,
  596. (u16)(uintptr_t)(t - a->targetdb));
  597. }
  598. if (dev_count == 0)
  599. seq_puts(m, "none\n");
  600. seq_puts(m, "\n");
  601. return 0;
  602. }
  603. int esas2r_release(struct Scsi_Host *sh)
  604. {
  605. esas2r_log_dev(ESAS2R_LOG_INFO, &(sh->shost_gendev),
  606. "esas2r_release() called");
  607. esas2r_cleanup(sh);
  608. if (sh->irq)
  609. free_irq(sh->irq, NULL);
  610. scsi_unregister(sh);
  611. return 0;
  612. }
  613. const char *esas2r_info(struct Scsi_Host *sh)
  614. {
  615. struct esas2r_adapter *a = (struct esas2r_adapter *)sh->hostdata;
  616. static char esas2r_info_str[512];
  617. esas2r_log_dev(ESAS2R_LOG_INFO, &(sh->shost_gendev),
  618. "esas2r_info() called");
  619. /*
  620. * if we haven't done so already, register as a char driver
  621. * and stick a node under "/proc/scsi/esas2r/ATTOnode"
  622. */
  623. if (esas2r_proc_major <= 0) {
  624. esas2r_proc_host = sh;
  625. esas2r_proc_major = register_chrdev(0, ESAS2R_DRVR_NAME,
  626. &esas2r_proc_fops);
  627. esas2r_log_dev(ESAS2R_LOG_DEBG, &(sh->shost_gendev),
  628. "register_chrdev (major %d)",
  629. esas2r_proc_major);
  630. if (esas2r_proc_major > 0) {
  631. struct proc_dir_entry *pde;
  632. pde = proc_create(ATTONODE_NAME, 0,
  633. sh->hostt->proc_dir,
  634. &esas2r_proc_fops);
  635. if (!pde) {
  636. esas2r_log_dev(ESAS2R_LOG_WARN,
  637. &(sh->shost_gendev),
  638. "failed to create_proc_entry");
  639. esas2r_proc_major = -1;
  640. }
  641. }
  642. }
  643. sprintf(esas2r_info_str,
  644. ESAS2R_LONGNAME " (bus 0x%02X, device 0x%02X, IRQ 0x%02X)"
  645. " driver version: "ESAS2R_VERSION_STR " firmware version: "
  646. "%s\n",
  647. a->pcid->bus->number, a->pcid->devfn, a->pcid->irq,
  648. a->fw_rev[0] ? a->fw_rev : "(none)");
  649. return esas2r_info_str;
  650. }
  651. /* Callback for building a request scatter/gather list */
  652. static u32 get_physaddr_from_sgc(struct esas2r_sg_context *sgc, u64 *addr)
  653. {
  654. u32 len;
  655. if (likely(sgc->cur_offset == sgc->exp_offset)) {
  656. /*
  657. * the normal case: caller used all bytes from previous call, so
  658. * expected offset is the same as the current offset.
  659. */
  660. if (sgc->sgel_count < sgc->num_sgel) {
  661. /* retrieve next segment, except for first time */
  662. if (sgc->exp_offset > (u8 *)0) {
  663. /* advance current segment */
  664. sgc->cur_sgel = sg_next(sgc->cur_sgel);
  665. ++(sgc->sgel_count);
  666. }
  667. len = sg_dma_len(sgc->cur_sgel);
  668. (*addr) = sg_dma_address(sgc->cur_sgel);
  669. /* save the total # bytes returned to caller so far */
  670. sgc->exp_offset += len;
  671. } else {
  672. len = 0;
  673. }
  674. } else if (sgc->cur_offset < sgc->exp_offset) {
  675. /*
  676. * caller did not use all bytes from previous call. need to
  677. * compute the address based on current segment.
  678. */
  679. len = sg_dma_len(sgc->cur_sgel);
  680. (*addr) = sg_dma_address(sgc->cur_sgel);
  681. sgc->exp_offset -= len;
  682. /* calculate PA based on prev segment address and offsets */
  683. *addr = *addr +
  684. (sgc->cur_offset - sgc->exp_offset);
  685. sgc->exp_offset += len;
  686. /* re-calculate length based on offset */
  687. len = lower_32_bits(
  688. sgc->exp_offset - sgc->cur_offset);
  689. } else { /* if ( sgc->cur_offset > sgc->exp_offset ) */
  690. /*
  691. * we don't expect the caller to skip ahead.
  692. * cur_offset will never exceed the len we return
  693. */
  694. len = 0;
  695. }
  696. return len;
  697. }
  698. int esas2r_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
  699. {
  700. struct esas2r_adapter *a =
  701. (struct esas2r_adapter *)cmd->device->host->hostdata;
  702. struct esas2r_request *rq;
  703. struct esas2r_sg_context sgc;
  704. unsigned bufflen;
  705. /* Assume success, if it fails we will fix the result later. */
  706. cmd->result = DID_OK << 16;
  707. if (unlikely(test_bit(AF_DEGRADED_MODE, &a->flags))) {
  708. cmd->result = DID_NO_CONNECT << 16;
  709. cmd->scsi_done(cmd);
  710. return 0;
  711. }
  712. rq = esas2r_alloc_request(a);
  713. if (unlikely(rq == NULL)) {
  714. esas2r_debug("esas2r_alloc_request failed");
  715. return SCSI_MLQUEUE_HOST_BUSY;
  716. }
  717. rq->cmd = cmd;
  718. bufflen = scsi_bufflen(cmd);
  719. if (likely(bufflen != 0)) {
  720. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  721. rq->vrq->scsi.flags |= cpu_to_le32(FCP_CMND_WRD);
  722. else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
  723. rq->vrq->scsi.flags |= cpu_to_le32(FCP_CMND_RDD);
  724. }
  725. memcpy(rq->vrq->scsi.cdb, cmd->cmnd, cmd->cmd_len);
  726. rq->vrq->scsi.length = cpu_to_le32(bufflen);
  727. rq->target_id = cmd->device->id;
  728. rq->vrq->scsi.flags |= cpu_to_le32(cmd->device->lun);
  729. rq->sense_buf = cmd->sense_buffer;
  730. rq->sense_len = SCSI_SENSE_BUFFERSIZE;
  731. esas2r_sgc_init(&sgc, a, rq, NULL);
  732. sgc.length = bufflen;
  733. sgc.cur_offset = NULL;
  734. sgc.cur_sgel = scsi_sglist(cmd);
  735. sgc.exp_offset = NULL;
  736. sgc.num_sgel = scsi_dma_map(cmd);
  737. sgc.sgel_count = 0;
  738. if (unlikely(sgc.num_sgel < 0)) {
  739. esas2r_free_request(a, rq);
  740. return SCSI_MLQUEUE_HOST_BUSY;
  741. }
  742. sgc.get_phys_addr = (PGETPHYSADDR)get_physaddr_from_sgc;
  743. if (unlikely(!esas2r_build_sg_list(a, rq, &sgc))) {
  744. scsi_dma_unmap(cmd);
  745. esas2r_free_request(a, rq);
  746. return SCSI_MLQUEUE_HOST_BUSY;
  747. }
  748. esas2r_debug("start request %p to %d:%d\n", rq, (int)cmd->device->id,
  749. (int)cmd->device->lun);
  750. esas2r_start_request(a, rq);
  751. return 0;
  752. }
  753. static void complete_task_management_request(struct esas2r_adapter *a,
  754. struct esas2r_request *rq)
  755. {
  756. (*rq->task_management_status_ptr) = rq->req_stat;
  757. esas2r_free_request(a, rq);
  758. }
  759. /**
  760. * Searches the specified queue for the specified queue for the command
  761. * to abort.
  762. *
  763. * @param [in] a
  764. * @param [in] abort_request
  765. * @param [in] cmd
  766. * t
  767. * @return 0 on failure, 1 if command was not found, 2 if command was found
  768. */
  769. static int esas2r_check_active_queue(struct esas2r_adapter *a,
  770. struct esas2r_request **abort_request,
  771. struct scsi_cmnd *cmd,
  772. struct list_head *queue)
  773. {
  774. bool found = false;
  775. struct esas2r_request *ar = *abort_request;
  776. struct esas2r_request *rq;
  777. struct list_head *element, *next;
  778. list_for_each_safe(element, next, queue) {
  779. rq = list_entry(element, struct esas2r_request, req_list);
  780. if (rq->cmd == cmd) {
  781. /* Found the request. See what to do with it. */
  782. if (queue == &a->active_list) {
  783. /*
  784. * We are searching the active queue, which
  785. * means that we need to send an abort request
  786. * to the firmware.
  787. */
  788. ar = esas2r_alloc_request(a);
  789. if (ar == NULL) {
  790. esas2r_log_dev(ESAS2R_LOG_WARN,
  791. &(a->host->shost_gendev),
  792. "unable to allocate an abort request for cmd %p",
  793. cmd);
  794. return 0; /* Failure */
  795. }
  796. /*
  797. * Task management request must be formatted
  798. * with a lock held.
  799. */
  800. ar->sense_len = 0;
  801. ar->vrq->scsi.length = 0;
  802. ar->target_id = rq->target_id;
  803. ar->vrq->scsi.flags |= cpu_to_le32(
  804. (u8)le32_to_cpu(rq->vrq->scsi.flags));
  805. memset(ar->vrq->scsi.cdb, 0,
  806. sizeof(ar->vrq->scsi.cdb));
  807. ar->vrq->scsi.flags |= cpu_to_le32(
  808. FCP_CMND_TRM);
  809. ar->vrq->scsi.u.abort_handle =
  810. rq->vrq->scsi.handle;
  811. } else {
  812. /*
  813. * The request is pending but not active on
  814. * the firmware. Just free it now and we'll
  815. * report the successful abort below.
  816. */
  817. list_del_init(&rq->req_list);
  818. esas2r_free_request(a, rq);
  819. }
  820. found = true;
  821. break;
  822. }
  823. }
  824. if (!found)
  825. return 1; /* Not found */
  826. return 2; /* found */
  827. }
  828. int esas2r_eh_abort(struct scsi_cmnd *cmd)
  829. {
  830. struct esas2r_adapter *a =
  831. (struct esas2r_adapter *)cmd->device->host->hostdata;
  832. struct esas2r_request *abort_request = NULL;
  833. unsigned long flags;
  834. struct list_head *queue;
  835. int result;
  836. esas2r_log(ESAS2R_LOG_INFO, "eh_abort (%p)", cmd);
  837. if (test_bit(AF_DEGRADED_MODE, &a->flags)) {
  838. cmd->result = DID_ABORT << 16;
  839. scsi_set_resid(cmd, 0);
  840. cmd->scsi_done(cmd);
  841. return SUCCESS;
  842. }
  843. spin_lock_irqsave(&a->queue_lock, flags);
  844. /*
  845. * Run through the defer and active queues looking for the request
  846. * to abort.
  847. */
  848. queue = &a->defer_list;
  849. check_active_queue:
  850. result = esas2r_check_active_queue(a, &abort_request, cmd, queue);
  851. if (!result) {
  852. spin_unlock_irqrestore(&a->queue_lock, flags);
  853. return FAILED;
  854. } else if (result == 2 && (queue == &a->defer_list)) {
  855. queue = &a->active_list;
  856. goto check_active_queue;
  857. }
  858. spin_unlock_irqrestore(&a->queue_lock, flags);
  859. if (abort_request) {
  860. u8 task_management_status = RS_PENDING;
  861. /*
  862. * the request is already active, so we need to tell
  863. * the firmware to abort it and wait for the response.
  864. */
  865. abort_request->comp_cb = complete_task_management_request;
  866. abort_request->task_management_status_ptr =
  867. &task_management_status;
  868. esas2r_start_request(a, abort_request);
  869. if (atomic_read(&a->disable_cnt) == 0)
  870. esas2r_do_deferred_processes(a);
  871. while (task_management_status == RS_PENDING)
  872. msleep(10);
  873. /*
  874. * Once we get here, the original request will have been
  875. * completed by the firmware and the abort request will have
  876. * been cleaned up. we're done!
  877. */
  878. return SUCCESS;
  879. }
  880. /*
  881. * If we get here, either we found the inactive request and
  882. * freed it, or we didn't find it at all. Either way, success!
  883. */
  884. cmd->result = DID_ABORT << 16;
  885. scsi_set_resid(cmd, 0);
  886. cmd->scsi_done(cmd);
  887. return SUCCESS;
  888. }
  889. static int esas2r_host_bus_reset(struct scsi_cmnd *cmd, bool host_reset)
  890. {
  891. struct esas2r_adapter *a =
  892. (struct esas2r_adapter *)cmd->device->host->hostdata;
  893. if (test_bit(AF_DEGRADED_MODE, &a->flags))
  894. return FAILED;
  895. if (host_reset)
  896. esas2r_reset_adapter(a);
  897. else
  898. esas2r_reset_bus(a);
  899. /* above call sets the AF_OS_RESET flag. wait for it to clear. */
  900. while (test_bit(AF_OS_RESET, &a->flags)) {
  901. msleep(10);
  902. if (test_bit(AF_DEGRADED_MODE, &a->flags))
  903. return FAILED;
  904. }
  905. if (test_bit(AF_DEGRADED_MODE, &a->flags))
  906. return FAILED;
  907. return SUCCESS;
  908. }
  909. int esas2r_host_reset(struct scsi_cmnd *cmd)
  910. {
  911. esas2r_log(ESAS2R_LOG_INFO, "host_reset (%p)", cmd);
  912. return esas2r_host_bus_reset(cmd, true);
  913. }
  914. int esas2r_bus_reset(struct scsi_cmnd *cmd)
  915. {
  916. esas2r_log(ESAS2R_LOG_INFO, "bus_reset (%p)", cmd);
  917. return esas2r_host_bus_reset(cmd, false);
  918. }
  919. static int esas2r_dev_targ_reset(struct scsi_cmnd *cmd, bool target_reset)
  920. {
  921. struct esas2r_adapter *a =
  922. (struct esas2r_adapter *)cmd->device->host->hostdata;
  923. struct esas2r_request *rq;
  924. u8 task_management_status = RS_PENDING;
  925. bool completed;
  926. if (test_bit(AF_DEGRADED_MODE, &a->flags))
  927. return FAILED;
  928. retry:
  929. rq = esas2r_alloc_request(a);
  930. if (rq == NULL) {
  931. if (target_reset) {
  932. esas2r_log(ESAS2R_LOG_CRIT,
  933. "unable to allocate a request for a "
  934. "target reset (%d)!",
  935. cmd->device->id);
  936. } else {
  937. esas2r_log(ESAS2R_LOG_CRIT,
  938. "unable to allocate a request for a "
  939. "device reset (%d:%d)!",
  940. cmd->device->id,
  941. cmd->device->lun);
  942. }
  943. return FAILED;
  944. }
  945. rq->target_id = cmd->device->id;
  946. rq->vrq->scsi.flags |= cpu_to_le32(cmd->device->lun);
  947. rq->req_stat = RS_PENDING;
  948. rq->comp_cb = complete_task_management_request;
  949. rq->task_management_status_ptr = &task_management_status;
  950. if (target_reset) {
  951. esas2r_debug("issuing target reset (%p) to id %d", rq,
  952. cmd->device->id);
  953. completed = esas2r_send_task_mgmt(a, rq, 0x20);
  954. } else {
  955. esas2r_debug("issuing device reset (%p) to id %d lun %d", rq,
  956. cmd->device->id, cmd->device->lun);
  957. completed = esas2r_send_task_mgmt(a, rq, 0x10);
  958. }
  959. if (completed) {
  960. /* Task management cmd completed right away, need to free it. */
  961. esas2r_free_request(a, rq);
  962. } else {
  963. /*
  964. * Wait for firmware to complete the request. Completion
  965. * callback will free it.
  966. */
  967. while (task_management_status == RS_PENDING)
  968. msleep(10);
  969. }
  970. if (test_bit(AF_DEGRADED_MODE, &a->flags))
  971. return FAILED;
  972. if (task_management_status == RS_BUSY) {
  973. /*
  974. * Busy, probably because we are flashing. Wait a bit and
  975. * try again.
  976. */
  977. msleep(100);
  978. goto retry;
  979. }
  980. return SUCCESS;
  981. }
  982. int esas2r_device_reset(struct scsi_cmnd *cmd)
  983. {
  984. esas2r_log(ESAS2R_LOG_INFO, "device_reset (%p)", cmd);
  985. return esas2r_dev_targ_reset(cmd, false);
  986. }
  987. int esas2r_target_reset(struct scsi_cmnd *cmd)
  988. {
  989. esas2r_log(ESAS2R_LOG_INFO, "target_reset (%p)", cmd);
  990. return esas2r_dev_targ_reset(cmd, true);
  991. }
  992. int esas2r_change_queue_depth(struct scsi_device *dev, int depth, int reason)
  993. {
  994. esas2r_log(ESAS2R_LOG_INFO, "change_queue_depth %p, %d", dev, depth);
  995. scsi_adjust_queue_depth(dev, depth);
  996. return dev->queue_depth;
  997. }
  998. void esas2r_log_request_failure(struct esas2r_adapter *a,
  999. struct esas2r_request *rq)
  1000. {
  1001. u8 reqstatus = rq->req_stat;
  1002. if (reqstatus == RS_SUCCESS)
  1003. return;
  1004. if (rq->vrq->scsi.function == VDA_FUNC_SCSI) {
  1005. if (reqstatus == RS_SCSI_ERROR) {
  1006. if (rq->func_rsp.scsi_rsp.sense_len >= 13) {
  1007. esas2r_log(ESAS2R_LOG_WARN,
  1008. "request failure - SCSI error %x ASC:%x ASCQ:%x CDB:%x",
  1009. rq->sense_buf[2], rq->sense_buf[12],
  1010. rq->sense_buf[13],
  1011. rq->vrq->scsi.cdb[0]);
  1012. } else {
  1013. esas2r_log(ESAS2R_LOG_WARN,
  1014. "request failure - SCSI error CDB:%x\n",
  1015. rq->vrq->scsi.cdb[0]);
  1016. }
  1017. } else if ((rq->vrq->scsi.cdb[0] != INQUIRY
  1018. && rq->vrq->scsi.cdb[0] != REPORT_LUNS)
  1019. || (reqstatus != RS_SEL
  1020. && reqstatus != RS_SEL2)) {
  1021. if ((reqstatus == RS_UNDERRUN) &&
  1022. (rq->vrq->scsi.cdb[0] == INQUIRY)) {
  1023. /* Don't log inquiry underruns */
  1024. } else {
  1025. esas2r_log(ESAS2R_LOG_WARN,
  1026. "request failure - cdb:%x reqstatus:%d target:%d",
  1027. rq->vrq->scsi.cdb[0], reqstatus,
  1028. rq->target_id);
  1029. }
  1030. }
  1031. }
  1032. }
  1033. void esas2r_wait_request(struct esas2r_adapter *a, struct esas2r_request *rq)
  1034. {
  1035. u32 starttime;
  1036. u32 timeout;
  1037. starttime = jiffies_to_msecs(jiffies);
  1038. timeout = rq->timeout ? rq->timeout : 5000;
  1039. while (true) {
  1040. esas2r_polled_interrupt(a);
  1041. if (rq->req_stat != RS_STARTED)
  1042. break;
  1043. schedule_timeout_interruptible(msecs_to_jiffies(100));
  1044. if ((jiffies_to_msecs(jiffies) - starttime) > timeout) {
  1045. esas2r_hdebug("request TMO");
  1046. esas2r_bugon();
  1047. rq->req_stat = RS_TIMEOUT;
  1048. esas2r_local_reset_adapter(a);
  1049. return;
  1050. }
  1051. }
  1052. }
  1053. u32 esas2r_map_data_window(struct esas2r_adapter *a, u32 addr_lo)
  1054. {
  1055. u32 offset = addr_lo & (MW_DATA_WINDOW_SIZE - 1);
  1056. u32 base = addr_lo & -(signed int)MW_DATA_WINDOW_SIZE;
  1057. if (a->window_base != base) {
  1058. esas2r_write_register_dword(a, MVR_PCI_WIN1_REMAP,
  1059. base | MVRPW1R_ENABLE);
  1060. esas2r_flush_register_dword(a, MVR_PCI_WIN1_REMAP);
  1061. a->window_base = base;
  1062. }
  1063. return offset;
  1064. }
  1065. /* Read a block of data from chip memory */
  1066. bool esas2r_read_mem_block(struct esas2r_adapter *a,
  1067. void *to,
  1068. u32 from,
  1069. u32 size)
  1070. {
  1071. u8 *end = (u8 *)to;
  1072. while (size) {
  1073. u32 len;
  1074. u32 offset;
  1075. u32 iatvr;
  1076. iatvr = (from & -(signed int)MW_DATA_WINDOW_SIZE);
  1077. esas2r_map_data_window(a, iatvr);
  1078. offset = from & (MW_DATA_WINDOW_SIZE - 1);
  1079. len = size;
  1080. if (len > MW_DATA_WINDOW_SIZE - offset)
  1081. len = MW_DATA_WINDOW_SIZE - offset;
  1082. from += len;
  1083. size -= len;
  1084. while (len--) {
  1085. *end++ = esas2r_read_data_byte(a, offset);
  1086. offset++;
  1087. }
  1088. }
  1089. return true;
  1090. }
  1091. void esas2r_nuxi_mgt_data(u8 function, void *data)
  1092. {
  1093. struct atto_vda_grp_info *g;
  1094. struct atto_vda_devinfo *d;
  1095. struct atto_vdapart_info *p;
  1096. struct atto_vda_dh_info *h;
  1097. struct atto_vda_metrics_info *m;
  1098. struct atto_vda_schedule_info *s;
  1099. struct atto_vda_buzzer_info *b;
  1100. u8 i;
  1101. switch (function) {
  1102. case VDAMGT_BUZZER_INFO:
  1103. case VDAMGT_BUZZER_SET:
  1104. b = (struct atto_vda_buzzer_info *)data;
  1105. b->duration = le32_to_cpu(b->duration);
  1106. break;
  1107. case VDAMGT_SCHEDULE_INFO:
  1108. case VDAMGT_SCHEDULE_EVENT:
  1109. s = (struct atto_vda_schedule_info *)data;
  1110. s->id = le32_to_cpu(s->id);
  1111. break;
  1112. case VDAMGT_DEV_INFO:
  1113. case VDAMGT_DEV_CLEAN:
  1114. case VDAMGT_DEV_PT_INFO:
  1115. case VDAMGT_DEV_FEATURES:
  1116. case VDAMGT_DEV_PT_FEATURES:
  1117. case VDAMGT_DEV_OPERATION:
  1118. d = (struct atto_vda_devinfo *)data;
  1119. d->capacity = le64_to_cpu(d->capacity);
  1120. d->block_size = le32_to_cpu(d->block_size);
  1121. d->ses_dev_index = le16_to_cpu(d->ses_dev_index);
  1122. d->target_id = le16_to_cpu(d->target_id);
  1123. d->lun = le16_to_cpu(d->lun);
  1124. d->features = le16_to_cpu(d->features);
  1125. break;
  1126. case VDAMGT_GRP_INFO:
  1127. case VDAMGT_GRP_CREATE:
  1128. case VDAMGT_GRP_DELETE:
  1129. case VDAMGT_ADD_STORAGE:
  1130. case VDAMGT_MEMBER_ADD:
  1131. case VDAMGT_GRP_COMMIT:
  1132. case VDAMGT_GRP_REBUILD:
  1133. case VDAMGT_GRP_COMMIT_INIT:
  1134. case VDAMGT_QUICK_RAID:
  1135. case VDAMGT_GRP_FEATURES:
  1136. case VDAMGT_GRP_COMMIT_INIT_AUTOMAP:
  1137. case VDAMGT_QUICK_RAID_INIT_AUTOMAP:
  1138. case VDAMGT_SPARE_LIST:
  1139. case VDAMGT_SPARE_ADD:
  1140. case VDAMGT_SPARE_REMOVE:
  1141. case VDAMGT_LOCAL_SPARE_ADD:
  1142. case VDAMGT_GRP_OPERATION:
  1143. g = (struct atto_vda_grp_info *)data;
  1144. g->capacity = le64_to_cpu(g->capacity);
  1145. g->block_size = le32_to_cpu(g->block_size);
  1146. g->interleave = le32_to_cpu(g->interleave);
  1147. g->features = le16_to_cpu(g->features);
  1148. for (i = 0; i < 32; i++)
  1149. g->members[i] = le16_to_cpu(g->members[i]);
  1150. break;
  1151. case VDAMGT_PART_INFO:
  1152. case VDAMGT_PART_MAP:
  1153. case VDAMGT_PART_UNMAP:
  1154. case VDAMGT_PART_AUTOMAP:
  1155. case VDAMGT_PART_SPLIT:
  1156. case VDAMGT_PART_MERGE:
  1157. p = (struct atto_vdapart_info *)data;
  1158. p->part_size = le64_to_cpu(p->part_size);
  1159. p->start_lba = le32_to_cpu(p->start_lba);
  1160. p->block_size = le32_to_cpu(p->block_size);
  1161. p->target_id = le16_to_cpu(p->target_id);
  1162. break;
  1163. case VDAMGT_DEV_HEALTH_REQ:
  1164. h = (struct atto_vda_dh_info *)data;
  1165. h->med_defect_cnt = le32_to_cpu(h->med_defect_cnt);
  1166. h->info_exc_cnt = le32_to_cpu(h->info_exc_cnt);
  1167. break;
  1168. case VDAMGT_DEV_METRICS:
  1169. m = (struct atto_vda_metrics_info *)data;
  1170. for (i = 0; i < 32; i++)
  1171. m->dev_indexes[i] = le16_to_cpu(m->dev_indexes[i]);
  1172. break;
  1173. default:
  1174. break;
  1175. }
  1176. }
  1177. void esas2r_nuxi_cfg_data(u8 function, void *data)
  1178. {
  1179. struct atto_vda_cfg_init *ci;
  1180. switch (function) {
  1181. case VDA_CFG_INIT:
  1182. case VDA_CFG_GET_INIT:
  1183. case VDA_CFG_GET_INIT2:
  1184. ci = (struct atto_vda_cfg_init *)data;
  1185. ci->date_time.year = le16_to_cpu(ci->date_time.year);
  1186. ci->sgl_page_size = le32_to_cpu(ci->sgl_page_size);
  1187. ci->vda_version = le32_to_cpu(ci->vda_version);
  1188. ci->epoch_time = le32_to_cpu(ci->epoch_time);
  1189. ci->ioctl_tunnel = le32_to_cpu(ci->ioctl_tunnel);
  1190. ci->num_targets_backend = le32_to_cpu(ci->num_targets_backend);
  1191. break;
  1192. default:
  1193. break;
  1194. }
  1195. }
  1196. void esas2r_nuxi_ae_data(union atto_vda_ae *ae)
  1197. {
  1198. struct atto_vda_ae_raid *r = &ae->raid;
  1199. struct atto_vda_ae_lu *l = &ae->lu;
  1200. switch (ae->hdr.bytype) {
  1201. case VDAAE_HDR_TYPE_RAID:
  1202. r->dwflags = le32_to_cpu(r->dwflags);
  1203. break;
  1204. case VDAAE_HDR_TYPE_LU:
  1205. l->dwevent = le32_to_cpu(l->dwevent);
  1206. l->wphys_target_id = le16_to_cpu(l->wphys_target_id);
  1207. l->id.tgtlun.wtarget_id = le16_to_cpu(l->id.tgtlun.wtarget_id);
  1208. if (l->hdr.bylength >= offsetof(struct atto_vda_ae_lu, id)
  1209. + sizeof(struct atto_vda_ae_lu_tgt_lun_raid)) {
  1210. l->id.tgtlun_raid.dwinterleave
  1211. = le32_to_cpu(l->id.tgtlun_raid.dwinterleave);
  1212. l->id.tgtlun_raid.dwblock_size
  1213. = le32_to_cpu(l->id.tgtlun_raid.dwblock_size);
  1214. }
  1215. break;
  1216. case VDAAE_HDR_TYPE_DISK:
  1217. default:
  1218. break;
  1219. }
  1220. }
  1221. void esas2r_free_request(struct esas2r_adapter *a, struct esas2r_request *rq)
  1222. {
  1223. unsigned long flags;
  1224. esas2r_rq_destroy_request(rq, a);
  1225. spin_lock_irqsave(&a->request_lock, flags);
  1226. list_add(&rq->comp_list, &a->avail_request);
  1227. spin_unlock_irqrestore(&a->request_lock, flags);
  1228. }
  1229. struct esas2r_request *esas2r_alloc_request(struct esas2r_adapter *a)
  1230. {
  1231. struct esas2r_request *rq;
  1232. unsigned long flags;
  1233. spin_lock_irqsave(&a->request_lock, flags);
  1234. if (unlikely(list_empty(&a->avail_request))) {
  1235. spin_unlock_irqrestore(&a->request_lock, flags);
  1236. return NULL;
  1237. }
  1238. rq = list_first_entry(&a->avail_request, struct esas2r_request,
  1239. comp_list);
  1240. list_del(&rq->comp_list);
  1241. spin_unlock_irqrestore(&a->request_lock, flags);
  1242. esas2r_rq_init_request(rq, a);
  1243. return rq;
  1244. }
  1245. void esas2r_complete_request_cb(struct esas2r_adapter *a,
  1246. struct esas2r_request *rq)
  1247. {
  1248. esas2r_debug("completing request %p\n", rq);
  1249. scsi_dma_unmap(rq->cmd);
  1250. if (unlikely(rq->req_stat != RS_SUCCESS)) {
  1251. esas2r_debug("[%x STATUS %x:%x (%x)]", rq->target_id,
  1252. rq->req_stat,
  1253. rq->func_rsp.scsi_rsp.scsi_stat,
  1254. rq->cmd);
  1255. rq->cmd->result =
  1256. ((esas2r_req_status_to_error(rq->req_stat) << 16)
  1257. | (rq->func_rsp.scsi_rsp.scsi_stat & STATUS_MASK));
  1258. if (rq->req_stat == RS_UNDERRUN)
  1259. scsi_set_resid(rq->cmd,
  1260. le32_to_cpu(rq->func_rsp.scsi_rsp.
  1261. residual_length));
  1262. else
  1263. scsi_set_resid(rq->cmd, 0);
  1264. }
  1265. rq->cmd->scsi_done(rq->cmd);
  1266. esas2r_free_request(a, rq);
  1267. }
  1268. /* Run tasklet to handle stuff outside of interrupt context. */
  1269. void esas2r_adapter_tasklet(unsigned long context)
  1270. {
  1271. struct esas2r_adapter *a = (struct esas2r_adapter *)context;
  1272. if (unlikely(test_bit(AF2_TIMER_TICK, &a->flags2))) {
  1273. clear_bit(AF2_TIMER_TICK, &a->flags2);
  1274. esas2r_timer_tick(a);
  1275. }
  1276. if (likely(test_bit(AF2_INT_PENDING, &a->flags2))) {
  1277. clear_bit(AF2_INT_PENDING, &a->flags2);
  1278. esas2r_adapter_interrupt(a);
  1279. }
  1280. if (esas2r_is_tasklet_pending(a))
  1281. esas2r_do_tasklet_tasks(a);
  1282. if (esas2r_is_tasklet_pending(a)
  1283. || (test_bit(AF2_INT_PENDING, &a->flags2))
  1284. || (test_bit(AF2_TIMER_TICK, &a->flags2))) {
  1285. clear_bit(AF_TASKLET_SCHEDULED, &a->flags);
  1286. esas2r_schedule_tasklet(a);
  1287. } else {
  1288. clear_bit(AF_TASKLET_SCHEDULED, &a->flags);
  1289. }
  1290. }
  1291. static void esas2r_timer_callback(unsigned long context);
  1292. void esas2r_kickoff_timer(struct esas2r_adapter *a)
  1293. {
  1294. init_timer(&a->timer);
  1295. a->timer.function = esas2r_timer_callback;
  1296. a->timer.data = (unsigned long)a;
  1297. a->timer.expires = jiffies +
  1298. msecs_to_jiffies(100);
  1299. add_timer(&a->timer);
  1300. }
  1301. static void esas2r_timer_callback(unsigned long context)
  1302. {
  1303. struct esas2r_adapter *a = (struct esas2r_adapter *)context;
  1304. set_bit(AF2_TIMER_TICK, &a->flags2);
  1305. esas2r_schedule_tasklet(a);
  1306. esas2r_kickoff_timer(a);
  1307. }
  1308. /*
  1309. * Firmware events need to be handled outside of interrupt context
  1310. * so we schedule a delayed_work to handle them.
  1311. */
  1312. static void
  1313. esas2r_free_fw_event(struct esas2r_fw_event_work *fw_event)
  1314. {
  1315. unsigned long flags;
  1316. struct esas2r_adapter *a = fw_event->a;
  1317. spin_lock_irqsave(&a->fw_event_lock, flags);
  1318. list_del(&fw_event->list);
  1319. kfree(fw_event);
  1320. spin_unlock_irqrestore(&a->fw_event_lock, flags);
  1321. }
  1322. void
  1323. esas2r_fw_event_off(struct esas2r_adapter *a)
  1324. {
  1325. unsigned long flags;
  1326. spin_lock_irqsave(&a->fw_event_lock, flags);
  1327. a->fw_events_off = 1;
  1328. spin_unlock_irqrestore(&a->fw_event_lock, flags);
  1329. }
  1330. void
  1331. esas2r_fw_event_on(struct esas2r_adapter *a)
  1332. {
  1333. unsigned long flags;
  1334. spin_lock_irqsave(&a->fw_event_lock, flags);
  1335. a->fw_events_off = 0;
  1336. spin_unlock_irqrestore(&a->fw_event_lock, flags);
  1337. }
  1338. static void esas2r_add_device(struct esas2r_adapter *a, u16 target_id)
  1339. {
  1340. int ret;
  1341. struct scsi_device *scsi_dev;
  1342. scsi_dev = scsi_device_lookup(a->host, 0, target_id, 0);
  1343. if (scsi_dev) {
  1344. esas2r_log_dev(
  1345. ESAS2R_LOG_WARN,
  1346. &(scsi_dev->
  1347. sdev_gendev),
  1348. "scsi device already exists at id %d", target_id);
  1349. scsi_device_put(scsi_dev);
  1350. } else {
  1351. esas2r_log_dev(
  1352. ESAS2R_LOG_INFO,
  1353. &(a->host->
  1354. shost_gendev),
  1355. "scsi_add_device() called for 0:%d:0",
  1356. target_id);
  1357. ret = scsi_add_device(a->host, 0, target_id, 0);
  1358. if (ret) {
  1359. esas2r_log_dev(
  1360. ESAS2R_LOG_CRIT,
  1361. &(a->host->
  1362. shost_gendev),
  1363. "scsi_add_device failed with %d for id %d",
  1364. ret, target_id);
  1365. }
  1366. }
  1367. }
  1368. static void esas2r_remove_device(struct esas2r_adapter *a, u16 target_id)
  1369. {
  1370. struct scsi_device *scsi_dev;
  1371. scsi_dev = scsi_device_lookup(a->host, 0, target_id, 0);
  1372. if (scsi_dev) {
  1373. scsi_device_set_state(scsi_dev, SDEV_OFFLINE);
  1374. esas2r_log_dev(
  1375. ESAS2R_LOG_INFO,
  1376. &(scsi_dev->
  1377. sdev_gendev),
  1378. "scsi_remove_device() called for 0:%d:0",
  1379. target_id);
  1380. scsi_remove_device(scsi_dev);
  1381. esas2r_log_dev(
  1382. ESAS2R_LOG_INFO,
  1383. &(scsi_dev->
  1384. sdev_gendev),
  1385. "scsi_device_put() called");
  1386. scsi_device_put(scsi_dev);
  1387. } else {
  1388. esas2r_log_dev(
  1389. ESAS2R_LOG_WARN,
  1390. &(a->host->shost_gendev),
  1391. "no target found at id %d",
  1392. target_id);
  1393. }
  1394. }
  1395. /*
  1396. * Sends a firmware asynchronous event to anyone who happens to be
  1397. * listening on the defined ATTO VDA event ports.
  1398. */
  1399. static void esas2r_send_ae_event(struct esas2r_fw_event_work *fw_event)
  1400. {
  1401. struct esas2r_vda_ae *ae = (struct esas2r_vda_ae *)fw_event->data;
  1402. char *type;
  1403. switch (ae->vda_ae.hdr.bytype) {
  1404. case VDAAE_HDR_TYPE_RAID:
  1405. type = "RAID group state change";
  1406. break;
  1407. case VDAAE_HDR_TYPE_LU:
  1408. type = "Mapped destination LU change";
  1409. break;
  1410. case VDAAE_HDR_TYPE_DISK:
  1411. type = "Physical disk inventory change";
  1412. break;
  1413. case VDAAE_HDR_TYPE_RESET:
  1414. type = "Firmware reset";
  1415. break;
  1416. case VDAAE_HDR_TYPE_LOG_INFO:
  1417. type = "Event Log message (INFO level)";
  1418. break;
  1419. case VDAAE_HDR_TYPE_LOG_WARN:
  1420. type = "Event Log message (WARN level)";
  1421. break;
  1422. case VDAAE_HDR_TYPE_LOG_CRIT:
  1423. type = "Event Log message (CRIT level)";
  1424. break;
  1425. case VDAAE_HDR_TYPE_LOG_FAIL:
  1426. type = "Event Log message (FAIL level)";
  1427. break;
  1428. case VDAAE_HDR_TYPE_NVC:
  1429. type = "NVCache change";
  1430. break;
  1431. case VDAAE_HDR_TYPE_TLG_INFO:
  1432. type = "Time stamped log message (INFO level)";
  1433. break;
  1434. case VDAAE_HDR_TYPE_TLG_WARN:
  1435. type = "Time stamped log message (WARN level)";
  1436. break;
  1437. case VDAAE_HDR_TYPE_TLG_CRIT:
  1438. type = "Time stamped log message (CRIT level)";
  1439. break;
  1440. case VDAAE_HDR_TYPE_PWRMGT:
  1441. type = "Power management";
  1442. break;
  1443. case VDAAE_HDR_TYPE_MUTE:
  1444. type = "Mute button pressed";
  1445. break;
  1446. case VDAAE_HDR_TYPE_DEV:
  1447. type = "Device attribute change";
  1448. break;
  1449. default:
  1450. type = "Unknown";
  1451. break;
  1452. }
  1453. esas2r_log(ESAS2R_LOG_WARN,
  1454. "An async event of type \"%s\" was received from the firmware. The event contents are:",
  1455. type);
  1456. esas2r_log_hexdump(ESAS2R_LOG_WARN, &ae->vda_ae,
  1457. ae->vda_ae.hdr.bylength);
  1458. }
  1459. static void
  1460. esas2r_firmware_event_work(struct work_struct *work)
  1461. {
  1462. struct esas2r_fw_event_work *fw_event =
  1463. container_of(work, struct esas2r_fw_event_work, work.work);
  1464. struct esas2r_adapter *a = fw_event->a;
  1465. u16 target_id = *(u16 *)&fw_event->data[0];
  1466. if (a->fw_events_off)
  1467. goto done;
  1468. switch (fw_event->type) {
  1469. case fw_event_null:
  1470. break; /* do nothing */
  1471. case fw_event_lun_change:
  1472. esas2r_remove_device(a, target_id);
  1473. esas2r_add_device(a, target_id);
  1474. break;
  1475. case fw_event_present:
  1476. esas2r_add_device(a, target_id);
  1477. break;
  1478. case fw_event_not_present:
  1479. esas2r_remove_device(a, target_id);
  1480. break;
  1481. case fw_event_vda_ae:
  1482. esas2r_send_ae_event(fw_event);
  1483. break;
  1484. }
  1485. done:
  1486. esas2r_free_fw_event(fw_event);
  1487. }
  1488. void esas2r_queue_fw_event(struct esas2r_adapter *a,
  1489. enum fw_event_type type,
  1490. void *data,
  1491. int data_sz)
  1492. {
  1493. struct esas2r_fw_event_work *fw_event;
  1494. unsigned long flags;
  1495. fw_event = kzalloc(sizeof(struct esas2r_fw_event_work), GFP_ATOMIC);
  1496. if (!fw_event) {
  1497. esas2r_log(ESAS2R_LOG_WARN,
  1498. "esas2r_queue_fw_event failed to alloc");
  1499. return;
  1500. }
  1501. if (type == fw_event_vda_ae) {
  1502. struct esas2r_vda_ae *ae =
  1503. (struct esas2r_vda_ae *)fw_event->data;
  1504. ae->signature = ESAS2R_VDA_EVENT_SIG;
  1505. ae->bus_number = a->pcid->bus->number;
  1506. ae->devfn = a->pcid->devfn;
  1507. memcpy(&ae->vda_ae, data, sizeof(ae->vda_ae));
  1508. } else {
  1509. memcpy(fw_event->data, data, data_sz);
  1510. }
  1511. fw_event->type = type;
  1512. fw_event->a = a;
  1513. spin_lock_irqsave(&a->fw_event_lock, flags);
  1514. list_add_tail(&fw_event->list, &a->fw_event_list);
  1515. INIT_DELAYED_WORK(&fw_event->work, esas2r_firmware_event_work);
  1516. queue_delayed_work_on(
  1517. smp_processor_id(), a->fw_event_q, &fw_event->work,
  1518. msecs_to_jiffies(1));
  1519. spin_unlock_irqrestore(&a->fw_event_lock, flags);
  1520. }
  1521. void esas2r_target_state_changed(struct esas2r_adapter *a, u16 targ_id,
  1522. u8 state)
  1523. {
  1524. if (state == TS_LUN_CHANGE)
  1525. esas2r_queue_fw_event(a, fw_event_lun_change, &targ_id,
  1526. sizeof(targ_id));
  1527. else if (state == TS_PRESENT)
  1528. esas2r_queue_fw_event(a, fw_event_present, &targ_id,
  1529. sizeof(targ_id));
  1530. else if (state == TS_NOT_PRESENT)
  1531. esas2r_queue_fw_event(a, fw_event_not_present, &targ_id,
  1532. sizeof(targ_id));
  1533. }
  1534. /* Translate status to a Linux SCSI mid-layer error code */
  1535. int esas2r_req_status_to_error(u8 req_stat)
  1536. {
  1537. switch (req_stat) {
  1538. case RS_OVERRUN:
  1539. case RS_UNDERRUN:
  1540. case RS_SUCCESS:
  1541. /*
  1542. * NOTE: SCSI mid-layer wants a good status for a SCSI error, because
  1543. * it will check the scsi_stat value in the completion anyway.
  1544. */
  1545. case RS_SCSI_ERROR:
  1546. return DID_OK;
  1547. case RS_SEL:
  1548. case RS_SEL2:
  1549. return DID_NO_CONNECT;
  1550. case RS_RESET:
  1551. return DID_RESET;
  1552. case RS_ABORTED:
  1553. return DID_ABORT;
  1554. case RS_BUSY:
  1555. return DID_BUS_BUSY;
  1556. }
  1557. /* everything else is just an error. */
  1558. return DID_ERROR;
  1559. }
  1560. module_init(esas2r_init);
  1561. module_exit(esas2r_exit);