ibmvscsi.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727
  1. /* ------------------------------------------------------------
  2. * ibmvscsi.c
  3. * (C) Copyright IBM Corporation 1994, 2004
  4. * Authors: Colin DeVilbiss (devilbis@us.ibm.com)
  5. * Santiago Leon (santil@us.ibm.com)
  6. * Dave Boutcher (sleddog@us.ibm.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (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. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. * USA
  22. *
  23. * ------------------------------------------------------------
  24. * Emulation of a SCSI host adapter for Virtual I/O devices
  25. *
  26. * This driver supports the SCSI adapter implemented by the IBM
  27. * Power5 firmware. That SCSI adapter is not a physical adapter,
  28. * but allows Linux SCSI peripheral drivers to directly
  29. * access devices in another logical partition on the physical system.
  30. *
  31. * The virtual adapter(s) are present in the open firmware device
  32. * tree just like real adapters.
  33. *
  34. * One of the capabilities provided on these systems is the ability
  35. * to DMA between partitions. The architecture states that for VSCSI,
  36. * the server side is allowed to DMA to and from the client. The client
  37. * is never trusted to DMA to or from the server directly.
  38. *
  39. * Messages are sent between partitions on a "Command/Response Queue"
  40. * (CRQ), which is just a buffer of 16 byte entries in the receiver's
  41. * Senders cannot access the buffer directly, but send messages by
  42. * making a hypervisor call and passing in the 16 bytes. The hypervisor
  43. * puts the message in the next 16 byte space in round-robbin fashion,
  44. * turns on the high order bit of the message (the valid bit), and
  45. * generates an interrupt to the receiver (if interrupts are turned on.)
  46. * The receiver just turns off the valid bit when they have copied out
  47. * the message.
  48. *
  49. * The VSCSI client builds a SCSI Remote Protocol (SRP) Information Unit
  50. * (IU) (as defined in the T10 standard available at www.t10.org), gets
  51. * a DMA address for the message, and sends it to the server as the
  52. * payload of a CRQ message. The server DMAs the SRP IU and processes it,
  53. * including doing any additional data transfers. When it is done, it
  54. * DMAs the SRP response back to the same address as the request came from,
  55. * and sends a CRQ message back to inform the client that the request has
  56. * completed.
  57. *
  58. * Note that some of the underlying infrastructure is different between
  59. * machines conforming to the "RS/6000 Platform Architecture" (RPA) and
  60. * the older iSeries hypervisor models. To support both, some low level
  61. * routines have been broken out into rpa_vscsi.c and iseries_vscsi.c.
  62. * The Makefile should pick one, not two, not zero, of these.
  63. *
  64. * TODO: This is currently pretty tied to the IBM i/pSeries hypervisor
  65. * interfaces. It would be really nice to abstract this above an RDMA
  66. * layer.
  67. */
  68. #include <linux/module.h>
  69. #include <linux/moduleparam.h>
  70. #include <linux/dma-mapping.h>
  71. #include <linux/delay.h>
  72. #include <asm/firmware.h>
  73. #include <asm/vio.h>
  74. #include <scsi/scsi.h>
  75. #include <scsi/scsi_cmnd.h>
  76. #include <scsi/scsi_host.h>
  77. #include <scsi/scsi_device.h>
  78. #include <scsi/scsi_transport_srp.h>
  79. #include "ibmvscsi.h"
  80. /* The values below are somewhat arbitrary default values, but
  81. * OS/400 will use 3 busses (disks, CDs, tapes, I think.)
  82. * Note that there are 3 bits of channel value, 6 bits of id, and
  83. * 5 bits of LUN.
  84. */
  85. static int max_id = 64;
  86. static int max_channel = 3;
  87. static int init_timeout = 5;
  88. static int max_requests = IBMVSCSI_MAX_REQUESTS_DEFAULT;
  89. static struct scsi_transport_template *ibmvscsi_transport_template;
  90. #define IBMVSCSI_VERSION "1.5.8"
  91. static struct ibmvscsi_ops *ibmvscsi_ops;
  92. MODULE_DESCRIPTION("IBM Virtual SCSI");
  93. MODULE_AUTHOR("Dave Boutcher");
  94. MODULE_LICENSE("GPL");
  95. MODULE_VERSION(IBMVSCSI_VERSION);
  96. module_param_named(max_id, max_id, int, S_IRUGO | S_IWUSR);
  97. MODULE_PARM_DESC(max_id, "Largest ID value for each channel");
  98. module_param_named(max_channel, max_channel, int, S_IRUGO | S_IWUSR);
  99. MODULE_PARM_DESC(max_channel, "Largest channel value");
  100. module_param_named(init_timeout, init_timeout, int, S_IRUGO | S_IWUSR);
  101. MODULE_PARM_DESC(init_timeout, "Initialization timeout in seconds");
  102. module_param_named(max_requests, max_requests, int, S_IRUGO | S_IWUSR);
  103. MODULE_PARM_DESC(max_requests, "Maximum requests for this adapter");
  104. /* ------------------------------------------------------------
  105. * Routines for the event pool and event structs
  106. */
  107. /**
  108. * initialize_event_pool: - Allocates and initializes the event pool for a host
  109. * @pool: event_pool to be initialized
  110. * @size: Number of events in pool
  111. * @hostdata: ibmvscsi_host_data who owns the event pool
  112. *
  113. * Returns zero on success.
  114. */
  115. static int initialize_event_pool(struct event_pool *pool,
  116. int size, struct ibmvscsi_host_data *hostdata)
  117. {
  118. int i;
  119. pool->size = size;
  120. pool->next = 0;
  121. pool->events = kcalloc(pool->size, sizeof(*pool->events), GFP_KERNEL);
  122. if (!pool->events)
  123. return -ENOMEM;
  124. pool->iu_storage =
  125. dma_alloc_coherent(hostdata->dev,
  126. pool->size * sizeof(*pool->iu_storage),
  127. &pool->iu_token, 0);
  128. if (!pool->iu_storage) {
  129. kfree(pool->events);
  130. return -ENOMEM;
  131. }
  132. for (i = 0; i < pool->size; ++i) {
  133. struct srp_event_struct *evt = &pool->events[i];
  134. memset(&evt->crq, 0x00, sizeof(evt->crq));
  135. atomic_set(&evt->free, 1);
  136. evt->crq.valid = 0x80;
  137. evt->crq.IU_length = sizeof(*evt->xfer_iu);
  138. evt->crq.IU_data_ptr = pool->iu_token +
  139. sizeof(*evt->xfer_iu) * i;
  140. evt->xfer_iu = pool->iu_storage + i;
  141. evt->hostdata = hostdata;
  142. evt->ext_list = NULL;
  143. evt->ext_list_token = 0;
  144. }
  145. return 0;
  146. }
  147. /**
  148. * release_event_pool: - Frees memory of an event pool of a host
  149. * @pool: event_pool to be released
  150. * @hostdata: ibmvscsi_host_data who owns the even pool
  151. *
  152. * Returns zero on success.
  153. */
  154. static void release_event_pool(struct event_pool *pool,
  155. struct ibmvscsi_host_data *hostdata)
  156. {
  157. int i, in_use = 0;
  158. for (i = 0; i < pool->size; ++i) {
  159. if (atomic_read(&pool->events[i].free) != 1)
  160. ++in_use;
  161. if (pool->events[i].ext_list) {
  162. dma_free_coherent(hostdata->dev,
  163. SG_ALL * sizeof(struct srp_direct_buf),
  164. pool->events[i].ext_list,
  165. pool->events[i].ext_list_token);
  166. }
  167. }
  168. if (in_use)
  169. dev_warn(hostdata->dev, "releasing event pool with %d "
  170. "events still in use?\n", in_use);
  171. kfree(pool->events);
  172. dma_free_coherent(hostdata->dev,
  173. pool->size * sizeof(*pool->iu_storage),
  174. pool->iu_storage, pool->iu_token);
  175. }
  176. /**
  177. * valid_event_struct: - Determines if event is valid.
  178. * @pool: event_pool that contains the event
  179. * @evt: srp_event_struct to be checked for validity
  180. *
  181. * Returns zero if event is invalid, one otherwise.
  182. */
  183. static int valid_event_struct(struct event_pool *pool,
  184. struct srp_event_struct *evt)
  185. {
  186. int index = evt - pool->events;
  187. if (index < 0 || index >= pool->size) /* outside of bounds */
  188. return 0;
  189. if (evt != pool->events + index) /* unaligned */
  190. return 0;
  191. return 1;
  192. }
  193. /**
  194. * ibmvscsi_free-event_struct: - Changes status of event to "free"
  195. * @pool: event_pool that contains the event
  196. * @evt: srp_event_struct to be modified
  197. *
  198. */
  199. static void free_event_struct(struct event_pool *pool,
  200. struct srp_event_struct *evt)
  201. {
  202. if (!valid_event_struct(pool, evt)) {
  203. dev_err(evt->hostdata->dev, "Freeing invalid event_struct %p "
  204. "(not in pool %p)\n", evt, pool->events);
  205. return;
  206. }
  207. if (atomic_inc_return(&evt->free) != 1) {
  208. dev_err(evt->hostdata->dev, "Freeing event_struct %p "
  209. "which is not in use!\n", evt);
  210. return;
  211. }
  212. }
  213. /**
  214. * get_evt_struct: - Gets the next free event in pool
  215. * @pool: event_pool that contains the events to be searched
  216. *
  217. * Returns the next event in "free" state, and NULL if none are free.
  218. * Note that no synchronization is done here, we assume the host_lock
  219. * will syncrhonze things.
  220. */
  221. static struct srp_event_struct *get_event_struct(struct event_pool *pool)
  222. {
  223. int i;
  224. int poolsize = pool->size;
  225. int offset = pool->next;
  226. for (i = 0; i < poolsize; i++) {
  227. offset = (offset + 1) % poolsize;
  228. if (!atomic_dec_if_positive(&pool->events[offset].free)) {
  229. pool->next = offset;
  230. return &pool->events[offset];
  231. }
  232. }
  233. printk(KERN_ERR "ibmvscsi: found no event struct in pool!\n");
  234. return NULL;
  235. }
  236. /**
  237. * init_event_struct: Initialize fields in an event struct that are always
  238. * required.
  239. * @evt: The event
  240. * @done: Routine to call when the event is responded to
  241. * @format: SRP or MAD format
  242. * @timeout: timeout value set in the CRQ
  243. */
  244. static void init_event_struct(struct srp_event_struct *evt_struct,
  245. void (*done) (struct srp_event_struct *),
  246. u8 format,
  247. int timeout)
  248. {
  249. evt_struct->cmnd = NULL;
  250. evt_struct->cmnd_done = NULL;
  251. evt_struct->sync_srp = NULL;
  252. evt_struct->crq.format = format;
  253. evt_struct->crq.timeout = timeout;
  254. evt_struct->done = done;
  255. }
  256. /* ------------------------------------------------------------
  257. * Routines for receiving SCSI responses from the hosting partition
  258. */
  259. /**
  260. * set_srp_direction: Set the fields in the srp related to data
  261. * direction and number of buffers based on the direction in
  262. * the scsi_cmnd and the number of buffers
  263. */
  264. static void set_srp_direction(struct scsi_cmnd *cmd,
  265. struct srp_cmd *srp_cmd,
  266. int numbuf)
  267. {
  268. u8 fmt;
  269. if (numbuf == 0)
  270. return;
  271. if (numbuf == 1)
  272. fmt = SRP_DATA_DESC_DIRECT;
  273. else {
  274. fmt = SRP_DATA_DESC_INDIRECT;
  275. numbuf = min(numbuf, MAX_INDIRECT_BUFS);
  276. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  277. srp_cmd->data_out_desc_cnt = numbuf;
  278. else
  279. srp_cmd->data_in_desc_cnt = numbuf;
  280. }
  281. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  282. srp_cmd->buf_fmt = fmt << 4;
  283. else
  284. srp_cmd->buf_fmt = fmt;
  285. }
  286. static void unmap_sg_list(int num_entries,
  287. struct device *dev,
  288. struct srp_direct_buf *md)
  289. {
  290. int i;
  291. for (i = 0; i < num_entries; ++i)
  292. dma_unmap_single(dev, md[i].va, md[i].len, DMA_BIDIRECTIONAL);
  293. }
  294. /**
  295. * unmap_cmd_data: - Unmap data pointed in srp_cmd based on the format
  296. * @cmd: srp_cmd whose additional_data member will be unmapped
  297. * @dev: device for which the memory is mapped
  298. *
  299. */
  300. static void unmap_cmd_data(struct srp_cmd *cmd,
  301. struct srp_event_struct *evt_struct,
  302. struct device *dev)
  303. {
  304. u8 out_fmt, in_fmt;
  305. out_fmt = cmd->buf_fmt >> 4;
  306. in_fmt = cmd->buf_fmt & ((1U << 4) - 1);
  307. if (out_fmt == SRP_NO_DATA_DESC && in_fmt == SRP_NO_DATA_DESC)
  308. return;
  309. else if (out_fmt == SRP_DATA_DESC_DIRECT ||
  310. in_fmt == SRP_DATA_DESC_DIRECT) {
  311. struct srp_direct_buf *data =
  312. (struct srp_direct_buf *) cmd->add_data;
  313. dma_unmap_single(dev, data->va, data->len, DMA_BIDIRECTIONAL);
  314. } else {
  315. struct srp_indirect_buf *indirect =
  316. (struct srp_indirect_buf *) cmd->add_data;
  317. int num_mapped = indirect->table_desc.len /
  318. sizeof(struct srp_direct_buf);
  319. if (num_mapped <= MAX_INDIRECT_BUFS) {
  320. unmap_sg_list(num_mapped, dev, &indirect->desc_list[0]);
  321. return;
  322. }
  323. unmap_sg_list(num_mapped, dev, evt_struct->ext_list);
  324. }
  325. }
  326. static int map_sg_list(struct scsi_cmnd *cmd, int nseg,
  327. struct srp_direct_buf *md)
  328. {
  329. int i;
  330. struct scatterlist *sg;
  331. u64 total_length = 0;
  332. scsi_for_each_sg(cmd, sg, nseg, i) {
  333. struct srp_direct_buf *descr = md + i;
  334. descr->va = sg_dma_address(sg);
  335. descr->len = sg_dma_len(sg);
  336. descr->key = 0;
  337. total_length += sg_dma_len(sg);
  338. }
  339. return total_length;
  340. }
  341. /**
  342. * map_sg_data: - Maps dma for a scatterlist and initializes decriptor fields
  343. * @cmd: Scsi_Cmnd with the scatterlist
  344. * @srp_cmd: srp_cmd that contains the memory descriptor
  345. * @dev: device for which to map dma memory
  346. *
  347. * Called by map_data_for_srp_cmd() when building srp cmd from scsi cmd.
  348. * Returns 1 on success.
  349. */
  350. static int map_sg_data(struct scsi_cmnd *cmd,
  351. struct srp_event_struct *evt_struct,
  352. struct srp_cmd *srp_cmd, struct device *dev)
  353. {
  354. int sg_mapped;
  355. u64 total_length = 0;
  356. struct srp_direct_buf *data =
  357. (struct srp_direct_buf *) srp_cmd->add_data;
  358. struct srp_indirect_buf *indirect =
  359. (struct srp_indirect_buf *) data;
  360. sg_mapped = scsi_dma_map(cmd);
  361. if (!sg_mapped)
  362. return 1;
  363. else if (sg_mapped < 0)
  364. return 0;
  365. set_srp_direction(cmd, srp_cmd, sg_mapped);
  366. /* special case; we can use a single direct descriptor */
  367. if (sg_mapped == 1) {
  368. map_sg_list(cmd, sg_mapped, data);
  369. return 1;
  370. }
  371. indirect->table_desc.va = 0;
  372. indirect->table_desc.len = sg_mapped * sizeof(struct srp_direct_buf);
  373. indirect->table_desc.key = 0;
  374. if (sg_mapped <= MAX_INDIRECT_BUFS) {
  375. total_length = map_sg_list(cmd, sg_mapped,
  376. &indirect->desc_list[0]);
  377. indirect->len = total_length;
  378. return 1;
  379. }
  380. /* get indirect table */
  381. if (!evt_struct->ext_list) {
  382. evt_struct->ext_list = (struct srp_direct_buf *)
  383. dma_alloc_coherent(dev,
  384. SG_ALL * sizeof(struct srp_direct_buf),
  385. &evt_struct->ext_list_token, 0);
  386. if (!evt_struct->ext_list) {
  387. sdev_printk(KERN_ERR, cmd->device,
  388. "Can't allocate memory for indirect table\n");
  389. return 0;
  390. }
  391. }
  392. total_length = map_sg_list(cmd, sg_mapped, evt_struct->ext_list);
  393. indirect->len = total_length;
  394. indirect->table_desc.va = evt_struct->ext_list_token;
  395. indirect->table_desc.len = sg_mapped * sizeof(indirect->desc_list[0]);
  396. memcpy(indirect->desc_list, evt_struct->ext_list,
  397. MAX_INDIRECT_BUFS * sizeof(struct srp_direct_buf));
  398. return 1;
  399. }
  400. /**
  401. * map_data_for_srp_cmd: - Calls functions to map data for srp cmds
  402. * @cmd: struct scsi_cmnd with the memory to be mapped
  403. * @srp_cmd: srp_cmd that contains the memory descriptor
  404. * @dev: dma device for which to map dma memory
  405. *
  406. * Called by scsi_cmd_to_srp_cmd() when converting scsi cmds to srp cmds
  407. * Returns 1 on success.
  408. */
  409. static int map_data_for_srp_cmd(struct scsi_cmnd *cmd,
  410. struct srp_event_struct *evt_struct,
  411. struct srp_cmd *srp_cmd, struct device *dev)
  412. {
  413. switch (cmd->sc_data_direction) {
  414. case DMA_FROM_DEVICE:
  415. case DMA_TO_DEVICE:
  416. break;
  417. case DMA_NONE:
  418. return 1;
  419. case DMA_BIDIRECTIONAL:
  420. sdev_printk(KERN_ERR, cmd->device,
  421. "Can't map DMA_BIDIRECTIONAL to read/write\n");
  422. return 0;
  423. default:
  424. sdev_printk(KERN_ERR, cmd->device,
  425. "Unknown data direction 0x%02x; can't map!\n",
  426. cmd->sc_data_direction);
  427. return 0;
  428. }
  429. return map_sg_data(cmd, evt_struct, srp_cmd, dev);
  430. }
  431. /**
  432. * purge_requests: Our virtual adapter just shut down. purge any sent requests
  433. * @hostdata: the adapter
  434. */
  435. static void purge_requests(struct ibmvscsi_host_data *hostdata, int error_code)
  436. {
  437. struct srp_event_struct *tmp_evt, *pos;
  438. unsigned long flags;
  439. spin_lock_irqsave(hostdata->host->host_lock, flags);
  440. list_for_each_entry_safe(tmp_evt, pos, &hostdata->sent, list) {
  441. list_del(&tmp_evt->list);
  442. del_timer(&tmp_evt->timer);
  443. if (tmp_evt->cmnd) {
  444. tmp_evt->cmnd->result = (error_code << 16);
  445. unmap_cmd_data(&tmp_evt->iu.srp.cmd,
  446. tmp_evt,
  447. tmp_evt->hostdata->dev);
  448. if (tmp_evt->cmnd_done)
  449. tmp_evt->cmnd_done(tmp_evt->cmnd);
  450. } else if (tmp_evt->done)
  451. tmp_evt->done(tmp_evt);
  452. free_event_struct(&tmp_evt->hostdata->pool, tmp_evt);
  453. }
  454. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  455. }
  456. /**
  457. * ibmvscsi_reset_host - Reset the connection to the server
  458. * @hostdata: struct ibmvscsi_host_data to reset
  459. */
  460. static void ibmvscsi_reset_host(struct ibmvscsi_host_data *hostdata)
  461. {
  462. scsi_block_requests(hostdata->host);
  463. atomic_set(&hostdata->request_limit, 0);
  464. purge_requests(hostdata, DID_ERROR);
  465. if ((ibmvscsi_ops->reset_crq_queue(&hostdata->queue, hostdata)) ||
  466. (ibmvscsi_ops->send_crq(hostdata, 0xC001000000000000LL, 0)) ||
  467. (vio_enable_interrupts(to_vio_dev(hostdata->dev)))) {
  468. atomic_set(&hostdata->request_limit, -1);
  469. dev_err(hostdata->dev, "error after reset\n");
  470. }
  471. scsi_unblock_requests(hostdata->host);
  472. }
  473. /**
  474. * ibmvscsi_timeout - Internal command timeout handler
  475. * @evt_struct: struct srp_event_struct that timed out
  476. *
  477. * Called when an internally generated command times out
  478. */
  479. static void ibmvscsi_timeout(struct srp_event_struct *evt_struct)
  480. {
  481. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  482. dev_err(hostdata->dev, "Command timed out (%x). Resetting connection\n",
  483. evt_struct->iu.srp.cmd.opcode);
  484. ibmvscsi_reset_host(hostdata);
  485. }
  486. /* ------------------------------------------------------------
  487. * Routines for sending and receiving SRPs
  488. */
  489. /**
  490. * ibmvscsi_send_srp_event: - Transforms event to u64 array and calls send_crq()
  491. * @evt_struct: evt_struct to be sent
  492. * @hostdata: ibmvscsi_host_data of host
  493. * @timeout: timeout in seconds - 0 means do not time command
  494. *
  495. * Returns the value returned from ibmvscsi_send_crq(). (Zero for success)
  496. * Note that this routine assumes that host_lock is held for synchronization
  497. */
  498. static int ibmvscsi_send_srp_event(struct srp_event_struct *evt_struct,
  499. struct ibmvscsi_host_data *hostdata,
  500. unsigned long timeout)
  501. {
  502. u64 *crq_as_u64 = (u64 *) &evt_struct->crq;
  503. int request_status = 0;
  504. int rc;
  505. /* If we have exhausted our request limit, just fail this request,
  506. * unless it is for a reset or abort.
  507. * Note that there are rare cases involving driver generated requests
  508. * (such as task management requests) that the mid layer may think we
  509. * can handle more requests (can_queue) when we actually can't
  510. */
  511. if (evt_struct->crq.format == VIOSRP_SRP_FORMAT) {
  512. request_status =
  513. atomic_dec_if_positive(&hostdata->request_limit);
  514. /* If request limit was -1 when we started, it is now even
  515. * less than that
  516. */
  517. if (request_status < -1)
  518. goto send_error;
  519. /* Otherwise, we may have run out of requests. */
  520. /* If request limit was 0 when we started the adapter is in the
  521. * process of performing a login with the server adapter, or
  522. * we may have run out of requests.
  523. */
  524. else if (request_status == -1 &&
  525. evt_struct->iu.srp.login_req.opcode != SRP_LOGIN_REQ)
  526. goto send_busy;
  527. /* Abort and reset calls should make it through.
  528. * Nothing except abort and reset should use the last two
  529. * slots unless we had two or less to begin with.
  530. */
  531. else if (request_status < 2 &&
  532. evt_struct->iu.srp.cmd.opcode != SRP_TSK_MGMT) {
  533. /* In the case that we have less than two requests
  534. * available, check the server limit as a combination
  535. * of the request limit and the number of requests
  536. * in-flight (the size of the send list). If the
  537. * server limit is greater than 2, return busy so
  538. * that the last two are reserved for reset and abort.
  539. */
  540. int server_limit = request_status;
  541. struct srp_event_struct *tmp_evt;
  542. list_for_each_entry(tmp_evt, &hostdata->sent, list) {
  543. server_limit++;
  544. }
  545. if (server_limit > 2)
  546. goto send_busy;
  547. }
  548. }
  549. /* Copy the IU into the transfer area */
  550. *evt_struct->xfer_iu = evt_struct->iu;
  551. evt_struct->xfer_iu->srp.rsp.tag = (u64)evt_struct;
  552. /* Add this to the sent list. We need to do this
  553. * before we actually send
  554. * in case it comes back REALLY fast
  555. */
  556. list_add_tail(&evt_struct->list, &hostdata->sent);
  557. init_timer(&evt_struct->timer);
  558. if (timeout) {
  559. evt_struct->timer.data = (unsigned long) evt_struct;
  560. evt_struct->timer.expires = jiffies + (timeout * HZ);
  561. evt_struct->timer.function = (void (*)(unsigned long))ibmvscsi_timeout;
  562. add_timer(&evt_struct->timer);
  563. }
  564. if ((rc =
  565. ibmvscsi_ops->send_crq(hostdata, crq_as_u64[0], crq_as_u64[1])) != 0) {
  566. list_del(&evt_struct->list);
  567. del_timer(&evt_struct->timer);
  568. dev_err(hostdata->dev, "send error %d\n", rc);
  569. atomic_inc(&hostdata->request_limit);
  570. goto send_error;
  571. }
  572. return 0;
  573. send_busy:
  574. unmap_cmd_data(&evt_struct->iu.srp.cmd, evt_struct, hostdata->dev);
  575. free_event_struct(&hostdata->pool, evt_struct);
  576. if (request_status != -1)
  577. atomic_inc(&hostdata->request_limit);
  578. return SCSI_MLQUEUE_HOST_BUSY;
  579. send_error:
  580. unmap_cmd_data(&evt_struct->iu.srp.cmd, evt_struct, hostdata->dev);
  581. if (evt_struct->cmnd != NULL) {
  582. evt_struct->cmnd->result = DID_ERROR << 16;
  583. evt_struct->cmnd_done(evt_struct->cmnd);
  584. } else if (evt_struct->done)
  585. evt_struct->done(evt_struct);
  586. free_event_struct(&hostdata->pool, evt_struct);
  587. return 0;
  588. }
  589. /**
  590. * handle_cmd_rsp: - Handle responses from commands
  591. * @evt_struct: srp_event_struct to be handled
  592. *
  593. * Used as a callback by when sending scsi cmds.
  594. * Gets called by ibmvscsi_handle_crq()
  595. */
  596. static void handle_cmd_rsp(struct srp_event_struct *evt_struct)
  597. {
  598. struct srp_rsp *rsp = &evt_struct->xfer_iu->srp.rsp;
  599. struct scsi_cmnd *cmnd = evt_struct->cmnd;
  600. if (unlikely(rsp->opcode != SRP_RSP)) {
  601. if (printk_ratelimit())
  602. dev_warn(evt_struct->hostdata->dev,
  603. "bad SRP RSP type %d\n", rsp->opcode);
  604. }
  605. if (cmnd) {
  606. cmnd->result = rsp->status;
  607. if (((cmnd->result >> 1) & 0x1f) == CHECK_CONDITION)
  608. memcpy(cmnd->sense_buffer,
  609. rsp->data,
  610. rsp->sense_data_len);
  611. unmap_cmd_data(&evt_struct->iu.srp.cmd,
  612. evt_struct,
  613. evt_struct->hostdata->dev);
  614. if (rsp->flags & SRP_RSP_FLAG_DOOVER)
  615. scsi_set_resid(cmnd, rsp->data_out_res_cnt);
  616. else if (rsp->flags & SRP_RSP_FLAG_DIOVER)
  617. scsi_set_resid(cmnd, rsp->data_in_res_cnt);
  618. }
  619. if (evt_struct->cmnd_done)
  620. evt_struct->cmnd_done(cmnd);
  621. }
  622. /**
  623. * lun_from_dev: - Returns the lun of the scsi device
  624. * @dev: struct scsi_device
  625. *
  626. */
  627. static inline u16 lun_from_dev(struct scsi_device *dev)
  628. {
  629. return (0x2 << 14) | (dev->id << 8) | (dev->channel << 5) | dev->lun;
  630. }
  631. /**
  632. * ibmvscsi_queue: - The queuecommand function of the scsi template
  633. * @cmd: struct scsi_cmnd to be executed
  634. * @done: Callback function to be called when cmd is completed
  635. */
  636. static int ibmvscsi_queuecommand(struct scsi_cmnd *cmnd,
  637. void (*done) (struct scsi_cmnd *))
  638. {
  639. struct srp_cmd *srp_cmd;
  640. struct srp_event_struct *evt_struct;
  641. struct srp_indirect_buf *indirect;
  642. struct ibmvscsi_host_data *hostdata = shost_priv(cmnd->device->host);
  643. u16 lun = lun_from_dev(cmnd->device);
  644. u8 out_fmt, in_fmt;
  645. evt_struct = get_event_struct(&hostdata->pool);
  646. if (!evt_struct)
  647. return SCSI_MLQUEUE_HOST_BUSY;
  648. /* Set up the actual SRP IU */
  649. srp_cmd = &evt_struct->iu.srp.cmd;
  650. memset(srp_cmd, 0x00, SRP_MAX_IU_LEN);
  651. srp_cmd->opcode = SRP_CMD;
  652. memcpy(srp_cmd->cdb, cmnd->cmnd, sizeof(cmnd->cmnd));
  653. srp_cmd->lun = ((u64) lun) << 48;
  654. if (!map_data_for_srp_cmd(cmnd, evt_struct, srp_cmd, hostdata->dev)) {
  655. sdev_printk(KERN_ERR, cmnd->device, "couldn't convert cmd to srp_cmd\n");
  656. free_event_struct(&hostdata->pool, evt_struct);
  657. return SCSI_MLQUEUE_HOST_BUSY;
  658. }
  659. init_event_struct(evt_struct,
  660. handle_cmd_rsp,
  661. VIOSRP_SRP_FORMAT,
  662. cmnd->timeout_per_command/HZ);
  663. evt_struct->cmnd = cmnd;
  664. evt_struct->cmnd_done = done;
  665. /* Fix up dma address of the buffer itself */
  666. indirect = (struct srp_indirect_buf *) srp_cmd->add_data;
  667. out_fmt = srp_cmd->buf_fmt >> 4;
  668. in_fmt = srp_cmd->buf_fmt & ((1U << 4) - 1);
  669. if ((in_fmt == SRP_DATA_DESC_INDIRECT ||
  670. out_fmt == SRP_DATA_DESC_INDIRECT) &&
  671. indirect->table_desc.va == 0) {
  672. indirect->table_desc.va = evt_struct->crq.IU_data_ptr +
  673. offsetof(struct srp_cmd, add_data) +
  674. offsetof(struct srp_indirect_buf, desc_list);
  675. }
  676. return ibmvscsi_send_srp_event(evt_struct, hostdata, 0);
  677. }
  678. /* ------------------------------------------------------------
  679. * Routines for driver initialization
  680. */
  681. /**
  682. * adapter_info_rsp: - Handle response to MAD adapter info request
  683. * @evt_struct: srp_event_struct with the response
  684. *
  685. * Used as a "done" callback by when sending adapter_info. Gets called
  686. * by ibmvscsi_handle_crq()
  687. */
  688. static void adapter_info_rsp(struct srp_event_struct *evt_struct)
  689. {
  690. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  691. dma_unmap_single(hostdata->dev,
  692. evt_struct->iu.mad.adapter_info.buffer,
  693. evt_struct->iu.mad.adapter_info.common.length,
  694. DMA_BIDIRECTIONAL);
  695. if (evt_struct->xfer_iu->mad.adapter_info.common.status) {
  696. dev_err(hostdata->dev, "error %d getting adapter info\n",
  697. evt_struct->xfer_iu->mad.adapter_info.common.status);
  698. } else {
  699. dev_info(hostdata->dev, "host srp version: %s, "
  700. "host partition %s (%d), OS %d, max io %u\n",
  701. hostdata->madapter_info.srp_version,
  702. hostdata->madapter_info.partition_name,
  703. hostdata->madapter_info.partition_number,
  704. hostdata->madapter_info.os_type,
  705. hostdata->madapter_info.port_max_txu[0]);
  706. if (hostdata->madapter_info.port_max_txu[0])
  707. hostdata->host->max_sectors =
  708. hostdata->madapter_info.port_max_txu[0] >> 9;
  709. if (hostdata->madapter_info.os_type == 3 &&
  710. strcmp(hostdata->madapter_info.srp_version, "1.6a") <= 0) {
  711. dev_err(hostdata->dev, "host (Ver. %s) doesn't support large transfers\n",
  712. hostdata->madapter_info.srp_version);
  713. dev_err(hostdata->dev, "limiting scatterlists to %d\n",
  714. MAX_INDIRECT_BUFS);
  715. hostdata->host->sg_tablesize = MAX_INDIRECT_BUFS;
  716. }
  717. }
  718. }
  719. /**
  720. * send_mad_adapter_info: - Sends the mad adapter info request
  721. * and stores the result so it can be retrieved with
  722. * sysfs. We COULD consider causing a failure if the
  723. * returned SRP version doesn't match ours.
  724. * @hostdata: ibmvscsi_host_data of host
  725. *
  726. * Returns zero if successful.
  727. */
  728. static void send_mad_adapter_info(struct ibmvscsi_host_data *hostdata)
  729. {
  730. struct viosrp_adapter_info *req;
  731. struct srp_event_struct *evt_struct;
  732. unsigned long flags;
  733. dma_addr_t addr;
  734. evt_struct = get_event_struct(&hostdata->pool);
  735. if (!evt_struct) {
  736. dev_err(hostdata->dev,
  737. "couldn't allocate an event for ADAPTER_INFO_REQ!\n");
  738. return;
  739. }
  740. init_event_struct(evt_struct,
  741. adapter_info_rsp,
  742. VIOSRP_MAD_FORMAT,
  743. init_timeout);
  744. req = &evt_struct->iu.mad.adapter_info;
  745. memset(req, 0x00, sizeof(*req));
  746. req->common.type = VIOSRP_ADAPTER_INFO_TYPE;
  747. req->common.length = sizeof(hostdata->madapter_info);
  748. req->buffer = addr = dma_map_single(hostdata->dev,
  749. &hostdata->madapter_info,
  750. sizeof(hostdata->madapter_info),
  751. DMA_BIDIRECTIONAL);
  752. if (dma_mapping_error(req->buffer)) {
  753. dev_err(hostdata->dev, "Unable to map request_buffer for adapter_info!\n");
  754. free_event_struct(&hostdata->pool, evt_struct);
  755. return;
  756. }
  757. spin_lock_irqsave(hostdata->host->host_lock, flags);
  758. if (ibmvscsi_send_srp_event(evt_struct, hostdata, init_timeout * 2)) {
  759. dev_err(hostdata->dev, "couldn't send ADAPTER_INFO_REQ!\n");
  760. dma_unmap_single(hostdata->dev,
  761. addr,
  762. sizeof(hostdata->madapter_info),
  763. DMA_BIDIRECTIONAL);
  764. }
  765. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  766. };
  767. /**
  768. * login_rsp: - Handle response to SRP login request
  769. * @evt_struct: srp_event_struct with the response
  770. *
  771. * Used as a "done" callback by when sending srp_login. Gets called
  772. * by ibmvscsi_handle_crq()
  773. */
  774. static void login_rsp(struct srp_event_struct *evt_struct)
  775. {
  776. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  777. switch (evt_struct->xfer_iu->srp.login_rsp.opcode) {
  778. case SRP_LOGIN_RSP: /* it worked! */
  779. break;
  780. case SRP_LOGIN_REJ: /* refused! */
  781. dev_info(hostdata->dev, "SRP_LOGIN_REJ reason %u\n",
  782. evt_struct->xfer_iu->srp.login_rej.reason);
  783. /* Login failed. */
  784. atomic_set(&hostdata->request_limit, -1);
  785. return;
  786. default:
  787. dev_err(hostdata->dev, "Invalid login response typecode 0x%02x!\n",
  788. evt_struct->xfer_iu->srp.login_rsp.opcode);
  789. /* Login failed. */
  790. atomic_set(&hostdata->request_limit, -1);
  791. return;
  792. }
  793. dev_info(hostdata->dev, "SRP_LOGIN succeeded\n");
  794. if (evt_struct->xfer_iu->srp.login_rsp.req_lim_delta < 0)
  795. dev_err(hostdata->dev, "Invalid request_limit.\n");
  796. /* Now we know what the real request-limit is.
  797. * This value is set rather than added to request_limit because
  798. * request_limit could have been set to -1 by this client.
  799. */
  800. atomic_set(&hostdata->request_limit,
  801. evt_struct->xfer_iu->srp.login_rsp.req_lim_delta);
  802. /* If we had any pending I/Os, kick them */
  803. scsi_unblock_requests(hostdata->host);
  804. send_mad_adapter_info(hostdata);
  805. return;
  806. }
  807. /**
  808. * send_srp_login: - Sends the srp login
  809. * @hostdata: ibmvscsi_host_data of host
  810. *
  811. * Returns zero if successful.
  812. */
  813. static int send_srp_login(struct ibmvscsi_host_data *hostdata)
  814. {
  815. int rc;
  816. unsigned long flags;
  817. struct srp_login_req *login;
  818. struct srp_event_struct *evt_struct = get_event_struct(&hostdata->pool);
  819. if (!evt_struct) {
  820. dev_err(hostdata->dev, "couldn't allocate an event for login req!\n");
  821. return FAILED;
  822. }
  823. init_event_struct(evt_struct,
  824. login_rsp,
  825. VIOSRP_SRP_FORMAT,
  826. init_timeout);
  827. login = &evt_struct->iu.srp.login_req;
  828. memset(login, 0x00, sizeof(struct srp_login_req));
  829. login->opcode = SRP_LOGIN_REQ;
  830. login->req_it_iu_len = sizeof(union srp_iu);
  831. login->req_buf_fmt = SRP_BUF_FORMAT_DIRECT | SRP_BUF_FORMAT_INDIRECT;
  832. spin_lock_irqsave(hostdata->host->host_lock, flags);
  833. /* Start out with a request limit of 0, since this is negotiated in
  834. * the login request we are just sending and login requests always
  835. * get sent by the driver regardless of request_limit.
  836. */
  837. atomic_set(&hostdata->request_limit, 0);
  838. rc = ibmvscsi_send_srp_event(evt_struct, hostdata, init_timeout * 2);
  839. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  840. dev_info(hostdata->dev, "sent SRP login\n");
  841. return rc;
  842. };
  843. /**
  844. * sync_completion: Signal that a synchronous command has completed
  845. * Note that after returning from this call, the evt_struct is freed.
  846. * the caller waiting on this completion shouldn't touch the evt_struct
  847. * again.
  848. */
  849. static void sync_completion(struct srp_event_struct *evt_struct)
  850. {
  851. /* copy the response back */
  852. if (evt_struct->sync_srp)
  853. *evt_struct->sync_srp = *evt_struct->xfer_iu;
  854. complete(&evt_struct->comp);
  855. }
  856. /**
  857. * ibmvscsi_abort: Abort a command...from scsi host template
  858. * send this over to the server and wait synchronously for the response
  859. */
  860. static int ibmvscsi_eh_abort_handler(struct scsi_cmnd *cmd)
  861. {
  862. struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
  863. struct srp_tsk_mgmt *tsk_mgmt;
  864. struct srp_event_struct *evt;
  865. struct srp_event_struct *tmp_evt, *found_evt;
  866. union viosrp_iu srp_rsp;
  867. int rsp_rc;
  868. unsigned long flags;
  869. u16 lun = lun_from_dev(cmd->device);
  870. /* First, find this command in our sent list so we can figure
  871. * out the correct tag
  872. */
  873. spin_lock_irqsave(hostdata->host->host_lock, flags);
  874. found_evt = NULL;
  875. list_for_each_entry(tmp_evt, &hostdata->sent, list) {
  876. if (tmp_evt->cmnd == cmd) {
  877. found_evt = tmp_evt;
  878. break;
  879. }
  880. }
  881. if (!found_evt) {
  882. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  883. return SUCCESS;
  884. }
  885. evt = get_event_struct(&hostdata->pool);
  886. if (evt == NULL) {
  887. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  888. sdev_printk(KERN_ERR, cmd->device, "failed to allocate abort event\n");
  889. return FAILED;
  890. }
  891. init_event_struct(evt,
  892. sync_completion,
  893. VIOSRP_SRP_FORMAT,
  894. init_timeout);
  895. tsk_mgmt = &evt->iu.srp.tsk_mgmt;
  896. /* Set up an abort SRP command */
  897. memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
  898. tsk_mgmt->opcode = SRP_TSK_MGMT;
  899. tsk_mgmt->lun = ((u64) lun) << 48;
  900. tsk_mgmt->tsk_mgmt_func = SRP_TSK_ABORT_TASK;
  901. tsk_mgmt->task_tag = (u64) found_evt;
  902. sdev_printk(KERN_INFO, cmd->device, "aborting command. lun 0x%lx, tag 0x%lx\n",
  903. tsk_mgmt->lun, tsk_mgmt->task_tag);
  904. evt->sync_srp = &srp_rsp;
  905. init_completion(&evt->comp);
  906. rsp_rc = ibmvscsi_send_srp_event(evt, hostdata, init_timeout * 2);
  907. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  908. if (rsp_rc != 0) {
  909. sdev_printk(KERN_ERR, cmd->device,
  910. "failed to send abort() event. rc=%d\n", rsp_rc);
  911. return FAILED;
  912. }
  913. wait_for_completion(&evt->comp);
  914. /* make sure we got a good response */
  915. if (unlikely(srp_rsp.srp.rsp.opcode != SRP_RSP)) {
  916. if (printk_ratelimit())
  917. sdev_printk(KERN_WARNING, cmd->device, "abort bad SRP RSP type %d\n",
  918. srp_rsp.srp.rsp.opcode);
  919. return FAILED;
  920. }
  921. if (srp_rsp.srp.rsp.flags & SRP_RSP_FLAG_RSPVALID)
  922. rsp_rc = *((int *)srp_rsp.srp.rsp.data);
  923. else
  924. rsp_rc = srp_rsp.srp.rsp.status;
  925. if (rsp_rc) {
  926. if (printk_ratelimit())
  927. sdev_printk(KERN_WARNING, cmd->device,
  928. "abort code %d for task tag 0x%lx\n",
  929. rsp_rc, tsk_mgmt->task_tag);
  930. return FAILED;
  931. }
  932. /* Because we dropped the spinlock above, it's possible
  933. * The event is no longer in our list. Make sure it didn't
  934. * complete while we were aborting
  935. */
  936. spin_lock_irqsave(hostdata->host->host_lock, flags);
  937. found_evt = NULL;
  938. list_for_each_entry(tmp_evt, &hostdata->sent, list) {
  939. if (tmp_evt->cmnd == cmd) {
  940. found_evt = tmp_evt;
  941. break;
  942. }
  943. }
  944. if (found_evt == NULL) {
  945. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  946. sdev_printk(KERN_INFO, cmd->device, "aborted task tag 0x%lx completed\n",
  947. tsk_mgmt->task_tag);
  948. return SUCCESS;
  949. }
  950. sdev_printk(KERN_INFO, cmd->device, "successfully aborted task tag 0x%lx\n",
  951. tsk_mgmt->task_tag);
  952. cmd->result = (DID_ABORT << 16);
  953. list_del(&found_evt->list);
  954. unmap_cmd_data(&found_evt->iu.srp.cmd, found_evt,
  955. found_evt->hostdata->dev);
  956. free_event_struct(&found_evt->hostdata->pool, found_evt);
  957. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  958. atomic_inc(&hostdata->request_limit);
  959. return SUCCESS;
  960. }
  961. /**
  962. * ibmvscsi_eh_device_reset_handler: Reset a single LUN...from scsi host
  963. * template send this over to the server and wait synchronously for the
  964. * response
  965. */
  966. static int ibmvscsi_eh_device_reset_handler(struct scsi_cmnd *cmd)
  967. {
  968. struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
  969. struct srp_tsk_mgmt *tsk_mgmt;
  970. struct srp_event_struct *evt;
  971. struct srp_event_struct *tmp_evt, *pos;
  972. union viosrp_iu srp_rsp;
  973. int rsp_rc;
  974. unsigned long flags;
  975. u16 lun = lun_from_dev(cmd->device);
  976. spin_lock_irqsave(hostdata->host->host_lock, flags);
  977. evt = get_event_struct(&hostdata->pool);
  978. if (evt == NULL) {
  979. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  980. sdev_printk(KERN_ERR, cmd->device, "failed to allocate reset event\n");
  981. return FAILED;
  982. }
  983. init_event_struct(evt,
  984. sync_completion,
  985. VIOSRP_SRP_FORMAT,
  986. init_timeout);
  987. tsk_mgmt = &evt->iu.srp.tsk_mgmt;
  988. /* Set up a lun reset SRP command */
  989. memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
  990. tsk_mgmt->opcode = SRP_TSK_MGMT;
  991. tsk_mgmt->lun = ((u64) lun) << 48;
  992. tsk_mgmt->tsk_mgmt_func = SRP_TSK_LUN_RESET;
  993. sdev_printk(KERN_INFO, cmd->device, "resetting device. lun 0x%lx\n",
  994. tsk_mgmt->lun);
  995. evt->sync_srp = &srp_rsp;
  996. init_completion(&evt->comp);
  997. rsp_rc = ibmvscsi_send_srp_event(evt, hostdata, init_timeout * 2);
  998. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  999. if (rsp_rc != 0) {
  1000. sdev_printk(KERN_ERR, cmd->device,
  1001. "failed to send reset event. rc=%d\n", rsp_rc);
  1002. return FAILED;
  1003. }
  1004. wait_for_completion(&evt->comp);
  1005. /* make sure we got a good response */
  1006. if (unlikely(srp_rsp.srp.rsp.opcode != SRP_RSP)) {
  1007. if (printk_ratelimit())
  1008. sdev_printk(KERN_WARNING, cmd->device, "reset bad SRP RSP type %d\n",
  1009. srp_rsp.srp.rsp.opcode);
  1010. return FAILED;
  1011. }
  1012. if (srp_rsp.srp.rsp.flags & SRP_RSP_FLAG_RSPVALID)
  1013. rsp_rc = *((int *)srp_rsp.srp.rsp.data);
  1014. else
  1015. rsp_rc = srp_rsp.srp.rsp.status;
  1016. if (rsp_rc) {
  1017. if (printk_ratelimit())
  1018. sdev_printk(KERN_WARNING, cmd->device,
  1019. "reset code %d for task tag 0x%lx\n",
  1020. rsp_rc, tsk_mgmt->task_tag);
  1021. return FAILED;
  1022. }
  1023. /* We need to find all commands for this LUN that have not yet been
  1024. * responded to, and fail them with DID_RESET
  1025. */
  1026. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1027. list_for_each_entry_safe(tmp_evt, pos, &hostdata->sent, list) {
  1028. if ((tmp_evt->cmnd) && (tmp_evt->cmnd->device == cmd->device)) {
  1029. if (tmp_evt->cmnd)
  1030. tmp_evt->cmnd->result = (DID_RESET << 16);
  1031. list_del(&tmp_evt->list);
  1032. unmap_cmd_data(&tmp_evt->iu.srp.cmd, tmp_evt,
  1033. tmp_evt->hostdata->dev);
  1034. free_event_struct(&tmp_evt->hostdata->pool,
  1035. tmp_evt);
  1036. atomic_inc(&hostdata->request_limit);
  1037. if (tmp_evt->cmnd_done)
  1038. tmp_evt->cmnd_done(tmp_evt->cmnd);
  1039. else if (tmp_evt->done)
  1040. tmp_evt->done(tmp_evt);
  1041. }
  1042. }
  1043. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1044. return SUCCESS;
  1045. }
  1046. /**
  1047. * ibmvscsi_eh_host_reset_handler - Reset the connection to the server
  1048. * @cmd: struct scsi_cmnd having problems
  1049. */
  1050. static int ibmvscsi_eh_host_reset_handler(struct scsi_cmnd *cmd)
  1051. {
  1052. unsigned long wait_switch = 0;
  1053. struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
  1054. dev_err(hostdata->dev, "Resetting connection due to error recovery\n");
  1055. ibmvscsi_reset_host(hostdata);
  1056. for (wait_switch = jiffies + (init_timeout * HZ);
  1057. time_before(jiffies, wait_switch) &&
  1058. atomic_read(&hostdata->request_limit) < 2;) {
  1059. msleep(10);
  1060. }
  1061. if (atomic_read(&hostdata->request_limit) <= 0)
  1062. return FAILED;
  1063. return SUCCESS;
  1064. }
  1065. /**
  1066. * ibmvscsi_handle_crq: - Handles and frees received events in the CRQ
  1067. * @crq: Command/Response queue
  1068. * @hostdata: ibmvscsi_host_data of host
  1069. *
  1070. */
  1071. void ibmvscsi_handle_crq(struct viosrp_crq *crq,
  1072. struct ibmvscsi_host_data *hostdata)
  1073. {
  1074. long rc;
  1075. unsigned long flags;
  1076. struct srp_event_struct *evt_struct =
  1077. (struct srp_event_struct *)crq->IU_data_ptr;
  1078. switch (crq->valid) {
  1079. case 0xC0: /* initialization */
  1080. switch (crq->format) {
  1081. case 0x01: /* Initialization message */
  1082. dev_info(hostdata->dev, "partner initialized\n");
  1083. /* Send back a response */
  1084. if ((rc = ibmvscsi_ops->send_crq(hostdata,
  1085. 0xC002000000000000LL, 0)) == 0) {
  1086. /* Now login */
  1087. send_srp_login(hostdata);
  1088. } else {
  1089. dev_err(hostdata->dev, "Unable to send init rsp. rc=%ld\n", rc);
  1090. }
  1091. break;
  1092. case 0x02: /* Initialization response */
  1093. dev_info(hostdata->dev, "partner initialization complete\n");
  1094. /* Now login */
  1095. send_srp_login(hostdata);
  1096. break;
  1097. default:
  1098. dev_err(hostdata->dev, "unknown crq message type: %d\n", crq->format);
  1099. }
  1100. return;
  1101. case 0xFF: /* Hypervisor telling us the connection is closed */
  1102. scsi_block_requests(hostdata->host);
  1103. atomic_set(&hostdata->request_limit, 0);
  1104. if (crq->format == 0x06) {
  1105. /* We need to re-setup the interpartition connection */
  1106. dev_info(hostdata->dev, "Re-enabling adapter!\n");
  1107. purge_requests(hostdata, DID_REQUEUE);
  1108. if ((ibmvscsi_ops->reenable_crq_queue(&hostdata->queue,
  1109. hostdata)) ||
  1110. (ibmvscsi_ops->send_crq(hostdata,
  1111. 0xC001000000000000LL, 0))) {
  1112. atomic_set(&hostdata->request_limit,
  1113. -1);
  1114. dev_err(hostdata->dev, "error after enable\n");
  1115. }
  1116. } else {
  1117. dev_err(hostdata->dev, "Virtual adapter failed rc %d!\n",
  1118. crq->format);
  1119. purge_requests(hostdata, DID_ERROR);
  1120. if ((ibmvscsi_ops->reset_crq_queue(&hostdata->queue,
  1121. hostdata)) ||
  1122. (ibmvscsi_ops->send_crq(hostdata,
  1123. 0xC001000000000000LL, 0))) {
  1124. atomic_set(&hostdata->request_limit,
  1125. -1);
  1126. dev_err(hostdata->dev, "error after reset\n");
  1127. }
  1128. }
  1129. scsi_unblock_requests(hostdata->host);
  1130. return;
  1131. case 0x80: /* real payload */
  1132. break;
  1133. default:
  1134. dev_err(hostdata->dev, "got an invalid message type 0x%02x\n",
  1135. crq->valid);
  1136. return;
  1137. }
  1138. /* The only kind of payload CRQs we should get are responses to
  1139. * things we send. Make sure this response is to something we
  1140. * actually sent
  1141. */
  1142. if (!valid_event_struct(&hostdata->pool, evt_struct)) {
  1143. dev_err(hostdata->dev, "returned correlation_token 0x%p is invalid!\n",
  1144. (void *)crq->IU_data_ptr);
  1145. return;
  1146. }
  1147. if (atomic_read(&evt_struct->free)) {
  1148. dev_err(hostdata->dev, "received duplicate correlation_token 0x%p!\n",
  1149. (void *)crq->IU_data_ptr);
  1150. return;
  1151. }
  1152. if (crq->format == VIOSRP_SRP_FORMAT)
  1153. atomic_add(evt_struct->xfer_iu->srp.rsp.req_lim_delta,
  1154. &hostdata->request_limit);
  1155. del_timer(&evt_struct->timer);
  1156. if (evt_struct->done)
  1157. evt_struct->done(evt_struct);
  1158. else
  1159. dev_err(hostdata->dev, "returned done() is NULL; not running it!\n");
  1160. /*
  1161. * Lock the host_lock before messing with these structures, since we
  1162. * are running in a task context
  1163. */
  1164. spin_lock_irqsave(evt_struct->hostdata->host->host_lock, flags);
  1165. list_del(&evt_struct->list);
  1166. free_event_struct(&evt_struct->hostdata->pool, evt_struct);
  1167. spin_unlock_irqrestore(evt_struct->hostdata->host->host_lock, flags);
  1168. }
  1169. /**
  1170. * ibmvscsi_get_host_config: Send the command to the server to get host
  1171. * configuration data. The data is opaque to us.
  1172. */
  1173. static int ibmvscsi_do_host_config(struct ibmvscsi_host_data *hostdata,
  1174. unsigned char *buffer, int length)
  1175. {
  1176. struct viosrp_host_config *host_config;
  1177. struct srp_event_struct *evt_struct;
  1178. unsigned long flags;
  1179. dma_addr_t addr;
  1180. int rc;
  1181. evt_struct = get_event_struct(&hostdata->pool);
  1182. if (!evt_struct) {
  1183. dev_err(hostdata->dev, "couldn't allocate event for HOST_CONFIG!\n");
  1184. return -1;
  1185. }
  1186. init_event_struct(evt_struct,
  1187. sync_completion,
  1188. VIOSRP_MAD_FORMAT,
  1189. init_timeout);
  1190. host_config = &evt_struct->iu.mad.host_config;
  1191. /* Set up a lun reset SRP command */
  1192. memset(host_config, 0x00, sizeof(*host_config));
  1193. host_config->common.type = VIOSRP_HOST_CONFIG_TYPE;
  1194. host_config->common.length = length;
  1195. host_config->buffer = addr = dma_map_single(hostdata->dev, buffer,
  1196. length,
  1197. DMA_BIDIRECTIONAL);
  1198. if (dma_mapping_error(host_config->buffer)) {
  1199. dev_err(hostdata->dev, "dma_mapping error getting host config\n");
  1200. free_event_struct(&hostdata->pool, evt_struct);
  1201. return -1;
  1202. }
  1203. init_completion(&evt_struct->comp);
  1204. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1205. rc = ibmvscsi_send_srp_event(evt_struct, hostdata, init_timeout * 2);
  1206. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1207. if (rc == 0)
  1208. wait_for_completion(&evt_struct->comp);
  1209. dma_unmap_single(hostdata->dev, addr, length, DMA_BIDIRECTIONAL);
  1210. return rc;
  1211. }
  1212. /**
  1213. * ibmvscsi_slave_configure: Set the "allow_restart" flag for each disk.
  1214. * @sdev: struct scsi_device device to configure
  1215. *
  1216. * Enable allow_restart for a device if it is a disk. Adjust the
  1217. * queue_depth here also as is required by the documentation for
  1218. * struct scsi_host_template.
  1219. */
  1220. static int ibmvscsi_slave_configure(struct scsi_device *sdev)
  1221. {
  1222. struct Scsi_Host *shost = sdev->host;
  1223. unsigned long lock_flags = 0;
  1224. spin_lock_irqsave(shost->host_lock, lock_flags);
  1225. if (sdev->type == TYPE_DISK) {
  1226. sdev->allow_restart = 1;
  1227. sdev->timeout = 60 * HZ;
  1228. }
  1229. scsi_adjust_queue_depth(sdev, 0, shost->cmd_per_lun);
  1230. spin_unlock_irqrestore(shost->host_lock, lock_flags);
  1231. return 0;
  1232. }
  1233. /**
  1234. * ibmvscsi_change_queue_depth - Change the device's queue depth
  1235. * @sdev: scsi device struct
  1236. * @qdepth: depth to set
  1237. *
  1238. * Return value:
  1239. * actual depth set
  1240. **/
  1241. static int ibmvscsi_change_queue_depth(struct scsi_device *sdev, int qdepth)
  1242. {
  1243. if (qdepth > IBMVSCSI_MAX_CMDS_PER_LUN)
  1244. qdepth = IBMVSCSI_MAX_CMDS_PER_LUN;
  1245. scsi_adjust_queue_depth(sdev, 0, qdepth);
  1246. return sdev->queue_depth;
  1247. }
  1248. /* ------------------------------------------------------------
  1249. * sysfs attributes
  1250. */
  1251. static ssize_t show_host_srp_version(struct class_device *class_dev, char *buf)
  1252. {
  1253. struct Scsi_Host *shost = class_to_shost(class_dev);
  1254. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1255. int len;
  1256. len = snprintf(buf, PAGE_SIZE, "%s\n",
  1257. hostdata->madapter_info.srp_version);
  1258. return len;
  1259. }
  1260. static struct class_device_attribute ibmvscsi_host_srp_version = {
  1261. .attr = {
  1262. .name = "srp_version",
  1263. .mode = S_IRUGO,
  1264. },
  1265. .show = show_host_srp_version,
  1266. };
  1267. static ssize_t show_host_partition_name(struct class_device *class_dev,
  1268. char *buf)
  1269. {
  1270. struct Scsi_Host *shost = class_to_shost(class_dev);
  1271. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1272. int len;
  1273. len = snprintf(buf, PAGE_SIZE, "%s\n",
  1274. hostdata->madapter_info.partition_name);
  1275. return len;
  1276. }
  1277. static struct class_device_attribute ibmvscsi_host_partition_name = {
  1278. .attr = {
  1279. .name = "partition_name",
  1280. .mode = S_IRUGO,
  1281. },
  1282. .show = show_host_partition_name,
  1283. };
  1284. static ssize_t show_host_partition_number(struct class_device *class_dev,
  1285. char *buf)
  1286. {
  1287. struct Scsi_Host *shost = class_to_shost(class_dev);
  1288. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1289. int len;
  1290. len = snprintf(buf, PAGE_SIZE, "%d\n",
  1291. hostdata->madapter_info.partition_number);
  1292. return len;
  1293. }
  1294. static struct class_device_attribute ibmvscsi_host_partition_number = {
  1295. .attr = {
  1296. .name = "partition_number",
  1297. .mode = S_IRUGO,
  1298. },
  1299. .show = show_host_partition_number,
  1300. };
  1301. static ssize_t show_host_mad_version(struct class_device *class_dev, char *buf)
  1302. {
  1303. struct Scsi_Host *shost = class_to_shost(class_dev);
  1304. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1305. int len;
  1306. len = snprintf(buf, PAGE_SIZE, "%d\n",
  1307. hostdata->madapter_info.mad_version);
  1308. return len;
  1309. }
  1310. static struct class_device_attribute ibmvscsi_host_mad_version = {
  1311. .attr = {
  1312. .name = "mad_version",
  1313. .mode = S_IRUGO,
  1314. },
  1315. .show = show_host_mad_version,
  1316. };
  1317. static ssize_t show_host_os_type(struct class_device *class_dev, char *buf)
  1318. {
  1319. struct Scsi_Host *shost = class_to_shost(class_dev);
  1320. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1321. int len;
  1322. len = snprintf(buf, PAGE_SIZE, "%d\n", hostdata->madapter_info.os_type);
  1323. return len;
  1324. }
  1325. static struct class_device_attribute ibmvscsi_host_os_type = {
  1326. .attr = {
  1327. .name = "os_type",
  1328. .mode = S_IRUGO,
  1329. },
  1330. .show = show_host_os_type,
  1331. };
  1332. static ssize_t show_host_config(struct class_device *class_dev, char *buf)
  1333. {
  1334. struct Scsi_Host *shost = class_to_shost(class_dev);
  1335. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1336. /* returns null-terminated host config data */
  1337. if (ibmvscsi_do_host_config(hostdata, buf, PAGE_SIZE) == 0)
  1338. return strlen(buf);
  1339. else
  1340. return 0;
  1341. }
  1342. static struct class_device_attribute ibmvscsi_host_config = {
  1343. .attr = {
  1344. .name = "config",
  1345. .mode = S_IRUGO,
  1346. },
  1347. .show = show_host_config,
  1348. };
  1349. static struct class_device_attribute *ibmvscsi_attrs[] = {
  1350. &ibmvscsi_host_srp_version,
  1351. &ibmvscsi_host_partition_name,
  1352. &ibmvscsi_host_partition_number,
  1353. &ibmvscsi_host_mad_version,
  1354. &ibmvscsi_host_os_type,
  1355. &ibmvscsi_host_config,
  1356. NULL
  1357. };
  1358. /* ------------------------------------------------------------
  1359. * SCSI driver registration
  1360. */
  1361. static struct scsi_host_template driver_template = {
  1362. .module = THIS_MODULE,
  1363. .name = "IBM POWER Virtual SCSI Adapter " IBMVSCSI_VERSION,
  1364. .proc_name = "ibmvscsi",
  1365. .queuecommand = ibmvscsi_queuecommand,
  1366. .eh_abort_handler = ibmvscsi_eh_abort_handler,
  1367. .eh_device_reset_handler = ibmvscsi_eh_device_reset_handler,
  1368. .eh_host_reset_handler = ibmvscsi_eh_host_reset_handler,
  1369. .slave_configure = ibmvscsi_slave_configure,
  1370. .change_queue_depth = ibmvscsi_change_queue_depth,
  1371. .cmd_per_lun = 16,
  1372. .can_queue = IBMVSCSI_MAX_REQUESTS_DEFAULT,
  1373. .this_id = -1,
  1374. .sg_tablesize = SG_ALL,
  1375. .use_clustering = ENABLE_CLUSTERING,
  1376. .use_sg_chaining = ENABLE_SG_CHAINING,
  1377. .shost_attrs = ibmvscsi_attrs,
  1378. };
  1379. /**
  1380. * Called by bus code for each adapter
  1381. */
  1382. static int ibmvscsi_probe(struct vio_dev *vdev, const struct vio_device_id *id)
  1383. {
  1384. struct ibmvscsi_host_data *hostdata;
  1385. struct Scsi_Host *host;
  1386. struct device *dev = &vdev->dev;
  1387. struct srp_rport_identifiers ids;
  1388. struct srp_rport *rport;
  1389. unsigned long wait_switch = 0;
  1390. int rc;
  1391. vdev->dev.driver_data = NULL;
  1392. driver_template.can_queue = max_requests;
  1393. host = scsi_host_alloc(&driver_template, sizeof(*hostdata));
  1394. if (!host) {
  1395. dev_err(&vdev->dev, "couldn't allocate host data\n");
  1396. goto scsi_host_alloc_failed;
  1397. }
  1398. host->transportt = ibmvscsi_transport_template;
  1399. hostdata = shost_priv(host);
  1400. memset(hostdata, 0x00, sizeof(*hostdata));
  1401. INIT_LIST_HEAD(&hostdata->sent);
  1402. hostdata->host = host;
  1403. hostdata->dev = dev;
  1404. atomic_set(&hostdata->request_limit, -1);
  1405. hostdata->host->max_sectors = 32 * 8; /* default max I/O 32 pages */
  1406. rc = ibmvscsi_ops->init_crq_queue(&hostdata->queue, hostdata, max_requests);
  1407. if (rc != 0 && rc != H_RESOURCE) {
  1408. dev_err(&vdev->dev, "couldn't initialize crq. rc=%d\n", rc);
  1409. goto init_crq_failed;
  1410. }
  1411. if (initialize_event_pool(&hostdata->pool, max_requests, hostdata) != 0) {
  1412. dev_err(&vdev->dev, "couldn't initialize event pool\n");
  1413. goto init_pool_failed;
  1414. }
  1415. host->max_lun = 8;
  1416. host->max_id = max_id;
  1417. host->max_channel = max_channel;
  1418. if (scsi_add_host(hostdata->host, hostdata->dev))
  1419. goto add_host_failed;
  1420. /* we don't have a proper target_port_id so let's use the fake one */
  1421. memcpy(ids.port_id, hostdata->madapter_info.partition_name,
  1422. sizeof(ids.port_id));
  1423. ids.roles = SRP_RPORT_ROLE_TARGET;
  1424. rport = srp_rport_add(host, &ids);
  1425. if (IS_ERR(rport))
  1426. goto add_srp_port_failed;
  1427. /* Try to send an initialization message. Note that this is allowed
  1428. * to fail if the other end is not acive. In that case we don't
  1429. * want to scan
  1430. */
  1431. if (ibmvscsi_ops->send_crq(hostdata, 0xC001000000000000LL, 0) == 0
  1432. || rc == H_RESOURCE) {
  1433. /*
  1434. * Wait around max init_timeout secs for the adapter to finish
  1435. * initializing. When we are done initializing, we will have a
  1436. * valid request_limit. We don't want Linux scanning before
  1437. * we are ready.
  1438. */
  1439. for (wait_switch = jiffies + (init_timeout * HZ);
  1440. time_before(jiffies, wait_switch) &&
  1441. atomic_read(&hostdata->request_limit) < 2;) {
  1442. msleep(10);
  1443. }
  1444. /* if we now have a valid request_limit, initiate a scan */
  1445. if (atomic_read(&hostdata->request_limit) > 0)
  1446. scsi_scan_host(host);
  1447. }
  1448. vdev->dev.driver_data = hostdata;
  1449. return 0;
  1450. add_srp_port_failed:
  1451. scsi_remove_host(hostdata->host);
  1452. add_host_failed:
  1453. release_event_pool(&hostdata->pool, hostdata);
  1454. init_pool_failed:
  1455. ibmvscsi_ops->release_crq_queue(&hostdata->queue, hostdata, max_requests);
  1456. init_crq_failed:
  1457. scsi_host_put(host);
  1458. scsi_host_alloc_failed:
  1459. return -1;
  1460. }
  1461. static int ibmvscsi_remove(struct vio_dev *vdev)
  1462. {
  1463. struct ibmvscsi_host_data *hostdata = vdev->dev.driver_data;
  1464. release_event_pool(&hostdata->pool, hostdata);
  1465. ibmvscsi_ops->release_crq_queue(&hostdata->queue, hostdata,
  1466. max_requests);
  1467. srp_remove_host(hostdata->host);
  1468. scsi_remove_host(hostdata->host);
  1469. scsi_host_put(hostdata->host);
  1470. return 0;
  1471. }
  1472. /**
  1473. * ibmvscsi_device_table: Used by vio.c to match devices in the device tree we
  1474. * support.
  1475. */
  1476. static struct vio_device_id ibmvscsi_device_table[] __devinitdata = {
  1477. {"vscsi", "IBM,v-scsi"},
  1478. { "", "" }
  1479. };
  1480. MODULE_DEVICE_TABLE(vio, ibmvscsi_device_table);
  1481. static struct vio_driver ibmvscsi_driver = {
  1482. .id_table = ibmvscsi_device_table,
  1483. .probe = ibmvscsi_probe,
  1484. .remove = ibmvscsi_remove,
  1485. .driver = {
  1486. .name = "ibmvscsi",
  1487. .owner = THIS_MODULE,
  1488. }
  1489. };
  1490. static struct srp_function_template ibmvscsi_transport_functions = {
  1491. };
  1492. int __init ibmvscsi_module_init(void)
  1493. {
  1494. int ret;
  1495. if (firmware_has_feature(FW_FEATURE_ISERIES))
  1496. ibmvscsi_ops = &iseriesvscsi_ops;
  1497. else if (firmware_has_feature(FW_FEATURE_VIO))
  1498. ibmvscsi_ops = &rpavscsi_ops;
  1499. else
  1500. return -ENODEV;
  1501. ibmvscsi_transport_template =
  1502. srp_attach_transport(&ibmvscsi_transport_functions);
  1503. if (!ibmvscsi_transport_template)
  1504. return -ENOMEM;
  1505. ret = vio_register_driver(&ibmvscsi_driver);
  1506. if (ret)
  1507. srp_release_transport(ibmvscsi_transport_template);
  1508. return ret;
  1509. }
  1510. void __exit ibmvscsi_module_exit(void)
  1511. {
  1512. vio_unregister_driver(&ibmvscsi_driver);
  1513. srp_release_transport(ibmvscsi_transport_template);
  1514. }
  1515. module_init(ibmvscsi_module_init);
  1516. module_exit(ibmvscsi_module_exit);