ibmvscsi.c 49 KB

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