scsi_transport_spi.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217
  1. /*
  2. * Parallel SCSI (SPI) transport specific attributes exported to sysfs.
  3. *
  4. * Copyright (c) 2003 Silicon Graphics, Inc. All rights reserved.
  5. * Copyright (c) 2004, 2005 James Bottomley <James.Bottomley@SteelEye.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/ctype.h>
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/workqueue.h>
  25. #include <linux/blkdev.h>
  26. #include <asm/semaphore.h>
  27. #include <scsi/scsi.h>
  28. #include "scsi_priv.h"
  29. #include <scsi/scsi_device.h>
  30. #include <scsi/scsi_host.h>
  31. #include <scsi/scsi_cmnd.h>
  32. #include <scsi/scsi_eh.h>
  33. #include <scsi/scsi_transport.h>
  34. #include <scsi/scsi_transport_spi.h>
  35. #define SPI_NUM_ATTRS 14 /* increase this if you add attributes */
  36. #define SPI_OTHER_ATTRS 1 /* Increase this if you add "always
  37. * on" attributes */
  38. #define SPI_HOST_ATTRS 1
  39. #define SPI_MAX_ECHO_BUFFER_SIZE 4096
  40. #define DV_LOOPS 3
  41. #define DV_TIMEOUT (10*HZ)
  42. #define DV_RETRIES 3 /* should only need at most
  43. * two cc/ua clears */
  44. /* Private data accessors (keep these out of the header file) */
  45. #define spi_dv_pending(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_pending)
  46. #define spi_dv_sem(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_sem)
  47. struct spi_internal {
  48. struct scsi_transport_template t;
  49. struct spi_function_template *f;
  50. /* The actual attributes */
  51. struct class_device_attribute private_attrs[SPI_NUM_ATTRS];
  52. /* The array of null terminated pointers to attributes
  53. * needed by scsi_sysfs.c */
  54. struct class_device_attribute *attrs[SPI_NUM_ATTRS + SPI_OTHER_ATTRS + 1];
  55. struct class_device_attribute private_host_attrs[SPI_HOST_ATTRS];
  56. struct class_device_attribute *host_attrs[SPI_HOST_ATTRS + 1];
  57. };
  58. #define to_spi_internal(tmpl) container_of(tmpl, struct spi_internal, t)
  59. static const int ppr_to_ps[] = {
  60. /* The PPR values 0-6 are reserved, fill them in when
  61. * the committee defines them */
  62. -1, /* 0x00 */
  63. -1, /* 0x01 */
  64. -1, /* 0x02 */
  65. -1, /* 0x03 */
  66. -1, /* 0x04 */
  67. -1, /* 0x05 */
  68. -1, /* 0x06 */
  69. 3125, /* 0x07 */
  70. 6250, /* 0x08 */
  71. 12500, /* 0x09 */
  72. 25000, /* 0x0a */
  73. 30300, /* 0x0b */
  74. 50000, /* 0x0c */
  75. };
  76. /* The PPR values at which you calculate the period in ns by multiplying
  77. * by 4 */
  78. #define SPI_STATIC_PPR 0x0c
  79. static int sprint_frac(char *dest, int value, int denom)
  80. {
  81. int frac = value % denom;
  82. int result = sprintf(dest, "%d", value / denom);
  83. if (frac == 0)
  84. return result;
  85. dest[result++] = '.';
  86. do {
  87. denom /= 10;
  88. sprintf(dest + result, "%d", frac / denom);
  89. result++;
  90. frac %= denom;
  91. } while (frac);
  92. dest[result++] = '\0';
  93. return result;
  94. }
  95. static int spi_execute(struct scsi_device *sdev, const void *cmd,
  96. enum dma_data_direction dir,
  97. void *buffer, unsigned bufflen,
  98. struct scsi_sense_hdr *sshdr)
  99. {
  100. int i, result;
  101. unsigned char sense[SCSI_SENSE_BUFFERSIZE];
  102. for(i = 0; i < DV_RETRIES; i++) {
  103. result = scsi_execute(sdev, cmd, dir, buffer, bufflen,
  104. sense, DV_TIMEOUT, /* retries */ 1,
  105. REQ_FAILFAST);
  106. if (result & DRIVER_SENSE) {
  107. struct scsi_sense_hdr sshdr_tmp;
  108. if (!sshdr)
  109. sshdr = &sshdr_tmp;
  110. if (scsi_normalize_sense(sense, sizeof(*sense),
  111. sshdr)
  112. && sshdr->sense_key == UNIT_ATTENTION)
  113. continue;
  114. }
  115. break;
  116. }
  117. return result;
  118. }
  119. static struct {
  120. enum spi_signal_type value;
  121. char *name;
  122. } signal_types[] = {
  123. { SPI_SIGNAL_UNKNOWN, "unknown" },
  124. { SPI_SIGNAL_SE, "SE" },
  125. { SPI_SIGNAL_LVD, "LVD" },
  126. { SPI_SIGNAL_HVD, "HVD" },
  127. };
  128. static inline const char *spi_signal_to_string(enum spi_signal_type type)
  129. {
  130. int i;
  131. for (i = 0; i < sizeof(signal_types)/sizeof(signal_types[0]); i++) {
  132. if (type == signal_types[i].value)
  133. return signal_types[i].name;
  134. }
  135. return NULL;
  136. }
  137. static inline enum spi_signal_type spi_signal_to_value(const char *name)
  138. {
  139. int i, len;
  140. for (i = 0; i < sizeof(signal_types)/sizeof(signal_types[0]); i++) {
  141. len = strlen(signal_types[i].name);
  142. if (strncmp(name, signal_types[i].name, len) == 0 &&
  143. (name[len] == '\n' || name[len] == '\0'))
  144. return signal_types[i].value;
  145. }
  146. return SPI_SIGNAL_UNKNOWN;
  147. }
  148. static int spi_host_setup(struct transport_container *tc, struct device *dev,
  149. struct class_device *cdev)
  150. {
  151. struct Scsi_Host *shost = dev_to_shost(dev);
  152. spi_signalling(shost) = SPI_SIGNAL_UNKNOWN;
  153. return 0;
  154. }
  155. static DECLARE_TRANSPORT_CLASS(spi_host_class,
  156. "spi_host",
  157. spi_host_setup,
  158. NULL,
  159. NULL);
  160. static int spi_host_match(struct attribute_container *cont,
  161. struct device *dev)
  162. {
  163. struct Scsi_Host *shost;
  164. struct spi_internal *i;
  165. if (!scsi_is_host_device(dev))
  166. return 0;
  167. shost = dev_to_shost(dev);
  168. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  169. != &spi_host_class.class)
  170. return 0;
  171. i = to_spi_internal(shost->transportt);
  172. return &i->t.host_attrs.ac == cont;
  173. }
  174. static int spi_device_configure(struct transport_container *tc,
  175. struct device *dev,
  176. struct class_device *cdev)
  177. {
  178. struct scsi_device *sdev = to_scsi_device(dev);
  179. struct scsi_target *starget = sdev->sdev_target;
  180. /* Populate the target capability fields with the values
  181. * gleaned from the device inquiry */
  182. spi_support_sync(starget) = scsi_device_sync(sdev);
  183. spi_support_wide(starget) = scsi_device_wide(sdev);
  184. spi_support_dt(starget) = scsi_device_dt(sdev);
  185. spi_support_dt_only(starget) = scsi_device_dt_only(sdev);
  186. spi_support_ius(starget) = scsi_device_ius(sdev);
  187. spi_support_qas(starget) = scsi_device_qas(sdev);
  188. return 0;
  189. }
  190. static int spi_setup_transport_attrs(struct transport_container *tc,
  191. struct device *dev,
  192. struct class_device *cdev)
  193. {
  194. struct scsi_target *starget = to_scsi_target(dev);
  195. spi_period(starget) = -1; /* illegal value */
  196. spi_min_period(starget) = 0;
  197. spi_offset(starget) = 0; /* async */
  198. spi_max_offset(starget) = 255;
  199. spi_width(starget) = 0; /* narrow */
  200. spi_max_width(starget) = 1;
  201. spi_iu(starget) = 0; /* no IU */
  202. spi_dt(starget) = 0; /* ST */
  203. spi_qas(starget) = 0;
  204. spi_wr_flow(starget) = 0;
  205. spi_rd_strm(starget) = 0;
  206. spi_rti(starget) = 0;
  207. spi_pcomp_en(starget) = 0;
  208. spi_hold_mcs(starget) = 0;
  209. spi_dv_pending(starget) = 0;
  210. spi_initial_dv(starget) = 0;
  211. init_MUTEX(&spi_dv_sem(starget));
  212. return 0;
  213. }
  214. #define spi_transport_show_simple(field, format_string) \
  215. \
  216. static ssize_t \
  217. show_spi_transport_##field(struct class_device *cdev, char *buf) \
  218. { \
  219. struct scsi_target *starget = transport_class_to_starget(cdev); \
  220. struct spi_transport_attrs *tp; \
  221. \
  222. tp = (struct spi_transport_attrs *)&starget->starget_data; \
  223. return snprintf(buf, 20, format_string, tp->field); \
  224. }
  225. #define spi_transport_store_simple(field, format_string) \
  226. \
  227. static ssize_t \
  228. store_spi_transport_##field(struct class_device *cdev, const char *buf, \
  229. size_t count) \
  230. { \
  231. int val; \
  232. struct scsi_target *starget = transport_class_to_starget(cdev); \
  233. struct spi_transport_attrs *tp; \
  234. \
  235. tp = (struct spi_transport_attrs *)&starget->starget_data; \
  236. val = simple_strtoul(buf, NULL, 0); \
  237. tp->field = val; \
  238. return count; \
  239. }
  240. #define spi_transport_show_function(field, format_string) \
  241. \
  242. static ssize_t \
  243. show_spi_transport_##field(struct class_device *cdev, char *buf) \
  244. { \
  245. struct scsi_target *starget = transport_class_to_starget(cdev); \
  246. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
  247. struct spi_transport_attrs *tp; \
  248. struct spi_internal *i = to_spi_internal(shost->transportt); \
  249. tp = (struct spi_transport_attrs *)&starget->starget_data; \
  250. if (i->f->get_##field) \
  251. i->f->get_##field(starget); \
  252. return snprintf(buf, 20, format_string, tp->field); \
  253. }
  254. #define spi_transport_store_function(field, format_string) \
  255. static ssize_t \
  256. store_spi_transport_##field(struct class_device *cdev, const char *buf, \
  257. size_t count) \
  258. { \
  259. int val; \
  260. struct scsi_target *starget = transport_class_to_starget(cdev); \
  261. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
  262. struct spi_internal *i = to_spi_internal(shost->transportt); \
  263. \
  264. val = simple_strtoul(buf, NULL, 0); \
  265. i->f->set_##field(starget, val); \
  266. return count; \
  267. }
  268. #define spi_transport_store_max(field, format_string) \
  269. static ssize_t \
  270. store_spi_transport_##field(struct class_device *cdev, const char *buf, \
  271. size_t count) \
  272. { \
  273. int val; \
  274. struct scsi_target *starget = transport_class_to_starget(cdev); \
  275. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
  276. struct spi_internal *i = to_spi_internal(shost->transportt); \
  277. struct spi_transport_attrs *tp \
  278. = (struct spi_transport_attrs *)&starget->starget_data; \
  279. \
  280. val = simple_strtoul(buf, NULL, 0); \
  281. if (val > tp->max_##field) \
  282. val = tp->max_##field; \
  283. i->f->set_##field(starget, val); \
  284. return count; \
  285. }
  286. #define spi_transport_rd_attr(field, format_string) \
  287. spi_transport_show_function(field, format_string) \
  288. spi_transport_store_function(field, format_string) \
  289. static CLASS_DEVICE_ATTR(field, S_IRUGO | S_IWUSR, \
  290. show_spi_transport_##field, \
  291. store_spi_transport_##field);
  292. #define spi_transport_simple_attr(field, format_string) \
  293. spi_transport_show_simple(field, format_string) \
  294. spi_transport_store_simple(field, format_string) \
  295. static CLASS_DEVICE_ATTR(field, S_IRUGO | S_IWUSR, \
  296. show_spi_transport_##field, \
  297. store_spi_transport_##field);
  298. #define spi_transport_max_attr(field, format_string) \
  299. spi_transport_show_function(field, format_string) \
  300. spi_transport_store_max(field, format_string) \
  301. spi_transport_simple_attr(max_##field, format_string) \
  302. static CLASS_DEVICE_ATTR(field, S_IRUGO | S_IWUSR, \
  303. show_spi_transport_##field, \
  304. store_spi_transport_##field);
  305. /* The Parallel SCSI Tranport Attributes: */
  306. spi_transport_max_attr(offset, "%d\n");
  307. spi_transport_max_attr(width, "%d\n");
  308. spi_transport_rd_attr(iu, "%d\n");
  309. spi_transport_rd_attr(dt, "%d\n");
  310. spi_transport_rd_attr(qas, "%d\n");
  311. spi_transport_rd_attr(wr_flow, "%d\n");
  312. spi_transport_rd_attr(rd_strm, "%d\n");
  313. spi_transport_rd_attr(rti, "%d\n");
  314. spi_transport_rd_attr(pcomp_en, "%d\n");
  315. spi_transport_rd_attr(hold_mcs, "%d\n");
  316. /* we only care about the first child device so we return 1 */
  317. static int child_iter(struct device *dev, void *data)
  318. {
  319. struct scsi_device *sdev = to_scsi_device(dev);
  320. spi_dv_device(sdev);
  321. return 1;
  322. }
  323. static ssize_t
  324. store_spi_revalidate(struct class_device *cdev, const char *buf, size_t count)
  325. {
  326. struct scsi_target *starget = transport_class_to_starget(cdev);
  327. device_for_each_child(&starget->dev, NULL, child_iter);
  328. return count;
  329. }
  330. static CLASS_DEVICE_ATTR(revalidate, S_IWUSR, NULL, store_spi_revalidate);
  331. /* Translate the period into ns according to the current spec
  332. * for SDTR/PPR messages */
  333. static ssize_t
  334. show_spi_transport_period_helper(struct class_device *cdev, char *buf,
  335. int period)
  336. {
  337. int len, picosec;
  338. if (period < 0 || period > 0xff) {
  339. picosec = -1;
  340. } else if (period <= SPI_STATIC_PPR) {
  341. picosec = ppr_to_ps[period];
  342. } else {
  343. picosec = period * 4000;
  344. }
  345. if (picosec == -1) {
  346. len = sprintf(buf, "reserved");
  347. } else {
  348. len = sprint_frac(buf, picosec, 1000);
  349. }
  350. buf[len++] = '\n';
  351. buf[len] = '\0';
  352. return len;
  353. }
  354. static ssize_t
  355. store_spi_transport_period_helper(struct class_device *cdev, const char *buf,
  356. size_t count, int *periodp)
  357. {
  358. int j, picosec, period = -1;
  359. char *endp;
  360. picosec = simple_strtoul(buf, &endp, 10) * 1000;
  361. if (*endp == '.') {
  362. int mult = 100;
  363. do {
  364. endp++;
  365. if (!isdigit(*endp))
  366. break;
  367. picosec += (*endp - '0') * mult;
  368. mult /= 10;
  369. } while (mult > 0);
  370. }
  371. for (j = 0; j <= SPI_STATIC_PPR; j++) {
  372. if (ppr_to_ps[j] < picosec)
  373. continue;
  374. period = j;
  375. break;
  376. }
  377. if (period == -1)
  378. period = picosec / 4000;
  379. if (period > 0xff)
  380. period = 0xff;
  381. *periodp = period;
  382. return count;
  383. }
  384. static ssize_t
  385. show_spi_transport_period(struct class_device *cdev, char *buf)
  386. {
  387. struct scsi_target *starget = transport_class_to_starget(cdev);
  388. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  389. struct spi_internal *i = to_spi_internal(shost->transportt);
  390. struct spi_transport_attrs *tp =
  391. (struct spi_transport_attrs *)&starget->starget_data;
  392. if (i->f->get_period)
  393. i->f->get_period(starget);
  394. return show_spi_transport_period_helper(cdev, buf, tp->period);
  395. }
  396. static ssize_t
  397. store_spi_transport_period(struct class_device *cdev, const char *buf,
  398. size_t count)
  399. {
  400. struct scsi_target *starget = transport_class_to_starget(cdev);
  401. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  402. struct spi_internal *i = to_spi_internal(shost->transportt);
  403. struct spi_transport_attrs *tp =
  404. (struct spi_transport_attrs *)&starget->starget_data;
  405. int period, retval;
  406. retval = store_spi_transport_period_helper(cdev, buf, count, &period);
  407. if (period < tp->min_period)
  408. period = tp->min_period;
  409. i->f->set_period(starget, period);
  410. return retval;
  411. }
  412. static CLASS_DEVICE_ATTR(period, S_IRUGO | S_IWUSR,
  413. show_spi_transport_period,
  414. store_spi_transport_period);
  415. static ssize_t
  416. show_spi_transport_min_period(struct class_device *cdev, char *buf)
  417. {
  418. struct scsi_target *starget = transport_class_to_starget(cdev);
  419. struct spi_transport_attrs *tp =
  420. (struct spi_transport_attrs *)&starget->starget_data;
  421. return show_spi_transport_period_helper(cdev, buf, tp->min_period);
  422. }
  423. static ssize_t
  424. store_spi_transport_min_period(struct class_device *cdev, const char *buf,
  425. size_t count)
  426. {
  427. struct scsi_target *starget = transport_class_to_starget(cdev);
  428. struct spi_transport_attrs *tp =
  429. (struct spi_transport_attrs *)&starget->starget_data;
  430. return store_spi_transport_period_helper(cdev, buf, count,
  431. &tp->min_period);
  432. }
  433. static CLASS_DEVICE_ATTR(min_period, S_IRUGO | S_IWUSR,
  434. show_spi_transport_min_period,
  435. store_spi_transport_min_period);
  436. static ssize_t show_spi_host_signalling(struct class_device *cdev, char *buf)
  437. {
  438. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  439. struct spi_internal *i = to_spi_internal(shost->transportt);
  440. if (i->f->get_signalling)
  441. i->f->get_signalling(shost);
  442. return sprintf(buf, "%s\n", spi_signal_to_string(spi_signalling(shost)));
  443. }
  444. static ssize_t store_spi_host_signalling(struct class_device *cdev,
  445. const char *buf, size_t count)
  446. {
  447. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  448. struct spi_internal *i = to_spi_internal(shost->transportt);
  449. enum spi_signal_type type = spi_signal_to_value(buf);
  450. if (type != SPI_SIGNAL_UNKNOWN)
  451. i->f->set_signalling(shost, type);
  452. return count;
  453. }
  454. static CLASS_DEVICE_ATTR(signalling, S_IRUGO | S_IWUSR,
  455. show_spi_host_signalling,
  456. store_spi_host_signalling);
  457. #define DV_SET(x, y) \
  458. if(i->f->set_##x) \
  459. i->f->set_##x(sdev->sdev_target, y)
  460. enum spi_compare_returns {
  461. SPI_COMPARE_SUCCESS,
  462. SPI_COMPARE_FAILURE,
  463. SPI_COMPARE_SKIP_TEST,
  464. };
  465. /* This is for read/write Domain Validation: If the device supports
  466. * an echo buffer, we do read/write tests to it */
  467. static enum spi_compare_returns
  468. spi_dv_device_echo_buffer(struct scsi_device *sdev, u8 *buffer,
  469. u8 *ptr, const int retries)
  470. {
  471. int len = ptr - buffer;
  472. int j, k, r, result;
  473. unsigned int pattern = 0x0000ffff;
  474. struct scsi_sense_hdr sshdr;
  475. const char spi_write_buffer[] = {
  476. WRITE_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
  477. };
  478. const char spi_read_buffer[] = {
  479. READ_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
  480. };
  481. /* set up the pattern buffer. Doesn't matter if we spill
  482. * slightly beyond since that's where the read buffer is */
  483. for (j = 0; j < len; ) {
  484. /* fill the buffer with counting (test a) */
  485. for ( ; j < min(len, 32); j++)
  486. buffer[j] = j;
  487. k = j;
  488. /* fill the buffer with alternating words of 0x0 and
  489. * 0xffff (test b) */
  490. for ( ; j < min(len, k + 32); j += 2) {
  491. u16 *word = (u16 *)&buffer[j];
  492. *word = (j & 0x02) ? 0x0000 : 0xffff;
  493. }
  494. k = j;
  495. /* fill with crosstalk (alternating 0x5555 0xaaa)
  496. * (test c) */
  497. for ( ; j < min(len, k + 32); j += 2) {
  498. u16 *word = (u16 *)&buffer[j];
  499. *word = (j & 0x02) ? 0x5555 : 0xaaaa;
  500. }
  501. k = j;
  502. /* fill with shifting bits (test d) */
  503. for ( ; j < min(len, k + 32); j += 4) {
  504. u32 *word = (unsigned int *)&buffer[j];
  505. u32 roll = (pattern & 0x80000000) ? 1 : 0;
  506. *word = pattern;
  507. pattern = (pattern << 1) | roll;
  508. }
  509. /* don't bother with random data (test e) */
  510. }
  511. for (r = 0; r < retries; r++) {
  512. result = spi_execute(sdev, spi_write_buffer, DMA_TO_DEVICE,
  513. buffer, len, &sshdr);
  514. if(result || !scsi_device_online(sdev)) {
  515. scsi_device_set_state(sdev, SDEV_QUIESCE);
  516. if (scsi_sense_valid(&sshdr)
  517. && sshdr.sense_key == ILLEGAL_REQUEST
  518. /* INVALID FIELD IN CDB */
  519. && sshdr.asc == 0x24 && sshdr.ascq == 0x00)
  520. /* This would mean that the drive lied
  521. * to us about supporting an echo
  522. * buffer (unfortunately some Western
  523. * Digital drives do precisely this)
  524. */
  525. return SPI_COMPARE_SKIP_TEST;
  526. sdev_printk(KERN_ERR, sdev, "Write Buffer failure %x\n", result);
  527. return SPI_COMPARE_FAILURE;
  528. }
  529. memset(ptr, 0, len);
  530. spi_execute(sdev, spi_read_buffer, DMA_FROM_DEVICE,
  531. ptr, len, NULL);
  532. scsi_device_set_state(sdev, SDEV_QUIESCE);
  533. if (memcmp(buffer, ptr, len) != 0)
  534. return SPI_COMPARE_FAILURE;
  535. }
  536. return SPI_COMPARE_SUCCESS;
  537. }
  538. /* This is for the simplest form of Domain Validation: a read test
  539. * on the inquiry data from the device */
  540. static enum spi_compare_returns
  541. spi_dv_device_compare_inquiry(struct scsi_device *sdev, u8 *buffer,
  542. u8 *ptr, const int retries)
  543. {
  544. int r, result;
  545. const int len = sdev->inquiry_len;
  546. const char spi_inquiry[] = {
  547. INQUIRY, 0, 0, 0, len, 0
  548. };
  549. for (r = 0; r < retries; r++) {
  550. memset(ptr, 0, len);
  551. result = spi_execute(sdev, spi_inquiry, DMA_FROM_DEVICE,
  552. ptr, len, NULL);
  553. if(result || !scsi_device_online(sdev)) {
  554. scsi_device_set_state(sdev, SDEV_QUIESCE);
  555. return SPI_COMPARE_FAILURE;
  556. }
  557. /* If we don't have the inquiry data already, the
  558. * first read gets it */
  559. if (ptr == buffer) {
  560. ptr += len;
  561. --r;
  562. continue;
  563. }
  564. if (memcmp(buffer, ptr, len) != 0)
  565. /* failure */
  566. return SPI_COMPARE_FAILURE;
  567. }
  568. return SPI_COMPARE_SUCCESS;
  569. }
  570. static enum spi_compare_returns
  571. spi_dv_retrain(struct scsi_device *sdev, u8 *buffer, u8 *ptr,
  572. enum spi_compare_returns
  573. (*compare_fn)(struct scsi_device *, u8 *, u8 *, int))
  574. {
  575. struct spi_internal *i = to_spi_internal(sdev->host->transportt);
  576. struct scsi_target *starget = sdev->sdev_target;
  577. int period = 0, prevperiod = 0;
  578. enum spi_compare_returns retval;
  579. for (;;) {
  580. int newperiod;
  581. retval = compare_fn(sdev, buffer, ptr, DV_LOOPS);
  582. if (retval == SPI_COMPARE_SUCCESS
  583. || retval == SPI_COMPARE_SKIP_TEST)
  584. break;
  585. /* OK, retrain, fallback */
  586. if (i->f->get_iu)
  587. i->f->get_iu(starget);
  588. if (i->f->get_qas)
  589. i->f->get_qas(starget);
  590. if (i->f->get_period)
  591. i->f->get_period(sdev->sdev_target);
  592. /* Here's the fallback sequence; first try turning off
  593. * IU, then QAS (if we can control them), then finally
  594. * fall down the periods */
  595. if (i->f->set_iu && spi_iu(starget)) {
  596. starget_printk(KERN_ERR, starget, "Domain Validation Disabing Information Units\n");
  597. DV_SET(iu, 0);
  598. } else if (i->f->set_qas && spi_qas(starget)) {
  599. starget_printk(KERN_ERR, starget, "Domain Validation Disabing Quick Arbitration and Selection\n");
  600. DV_SET(qas, 0);
  601. } else {
  602. newperiod = spi_period(starget);
  603. period = newperiod > period ? newperiod : period;
  604. if (period < 0x0d)
  605. period++;
  606. else
  607. period += period >> 1;
  608. if (unlikely(period > 0xff || period == prevperiod)) {
  609. /* Total failure; set to async and return */
  610. starget_printk(KERN_ERR, starget, "Domain Validation Failure, dropping back to Asynchronous\n");
  611. DV_SET(offset, 0);
  612. return SPI_COMPARE_FAILURE;
  613. }
  614. starget_printk(KERN_ERR, starget, "Domain Validation detected failure, dropping back\n");
  615. DV_SET(period, period);
  616. prevperiod = period;
  617. }
  618. }
  619. return retval;
  620. }
  621. static int
  622. spi_dv_device_get_echo_buffer(struct scsi_device *sdev, u8 *buffer)
  623. {
  624. int l, result;
  625. /* first off do a test unit ready. This can error out
  626. * because of reservations or some other reason. If it
  627. * fails, the device won't let us write to the echo buffer
  628. * so just return failure */
  629. const char spi_test_unit_ready[] = {
  630. TEST_UNIT_READY, 0, 0, 0, 0, 0
  631. };
  632. const char spi_read_buffer_descriptor[] = {
  633. READ_BUFFER, 0x0b, 0, 0, 0, 0, 0, 0, 4, 0
  634. };
  635. /* We send a set of three TURs to clear any outstanding
  636. * unit attention conditions if they exist (Otherwise the
  637. * buffer tests won't be happy). If the TUR still fails
  638. * (reservation conflict, device not ready, etc) just
  639. * skip the write tests */
  640. for (l = 0; ; l++) {
  641. result = spi_execute(sdev, spi_test_unit_ready, DMA_NONE,
  642. NULL, 0, NULL);
  643. if(result) {
  644. if(l >= 3)
  645. return 0;
  646. } else {
  647. /* TUR succeeded */
  648. break;
  649. }
  650. }
  651. result = spi_execute(sdev, spi_read_buffer_descriptor,
  652. DMA_FROM_DEVICE, buffer, 4, NULL);
  653. if (result)
  654. /* Device has no echo buffer */
  655. return 0;
  656. return buffer[3] + ((buffer[2] & 0x1f) << 8);
  657. }
  658. static void
  659. spi_dv_device_internal(struct scsi_device *sdev, u8 *buffer)
  660. {
  661. struct spi_internal *i = to_spi_internal(sdev->host->transportt);
  662. struct scsi_target *starget = sdev->sdev_target;
  663. int len = sdev->inquiry_len;
  664. /* first set us up for narrow async */
  665. DV_SET(offset, 0);
  666. DV_SET(width, 0);
  667. if (spi_dv_device_compare_inquiry(sdev, buffer, buffer, DV_LOOPS)
  668. != SPI_COMPARE_SUCCESS) {
  669. starget_printk(KERN_ERR, starget, "Domain Validation Initial Inquiry Failed\n");
  670. /* FIXME: should probably offline the device here? */
  671. return;
  672. }
  673. /* test width */
  674. if (i->f->set_width && spi_max_width(starget) &&
  675. scsi_device_wide(sdev)) {
  676. i->f->set_width(starget, 1);
  677. if (spi_dv_device_compare_inquiry(sdev, buffer,
  678. buffer + len,
  679. DV_LOOPS)
  680. != SPI_COMPARE_SUCCESS) {
  681. starget_printk(KERN_ERR, starget, "Wide Transfers Fail\n");
  682. i->f->set_width(starget, 0);
  683. }
  684. }
  685. if (!i->f->set_period)
  686. return;
  687. /* device can't handle synchronous */
  688. if (!scsi_device_sync(sdev) && !scsi_device_dt(sdev))
  689. return;
  690. /* see if the device has an echo buffer. If it does we can
  691. * do the SPI pattern write tests */
  692. len = 0;
  693. if (scsi_device_dt(sdev))
  694. len = spi_dv_device_get_echo_buffer(sdev, buffer);
  695. retry:
  696. /* now set up to the maximum */
  697. DV_SET(offset, spi_max_offset(starget));
  698. DV_SET(period, spi_min_period(starget));
  699. /* try QAS requests; this should be harmless to set if the
  700. * target supports it */
  701. if (scsi_device_qas(sdev))
  702. DV_SET(qas, 1);
  703. /* Also try IU transfers */
  704. if (scsi_device_ius(sdev))
  705. DV_SET(iu, 1);
  706. if (spi_min_period(starget) < 9) {
  707. /* This u320 (or u640). Ignore the coupled parameters
  708. * like DT and IU, but set the optional ones */
  709. DV_SET(rd_strm, 1);
  710. DV_SET(wr_flow, 1);
  711. DV_SET(rti, 1);
  712. if (spi_min_period(starget) == 8)
  713. DV_SET(pcomp_en, 1);
  714. }
  715. if (len == 0) {
  716. starget_printk(KERN_INFO, starget, "Domain Validation skipping write tests\n");
  717. spi_dv_retrain(sdev, buffer, buffer + len,
  718. spi_dv_device_compare_inquiry);
  719. return;
  720. }
  721. if (len > SPI_MAX_ECHO_BUFFER_SIZE) {
  722. starget_printk(KERN_WARNING, starget, "Echo buffer size %d is too big, trimming to %d\n", len, SPI_MAX_ECHO_BUFFER_SIZE);
  723. len = SPI_MAX_ECHO_BUFFER_SIZE;
  724. }
  725. if (spi_dv_retrain(sdev, buffer, buffer + len,
  726. spi_dv_device_echo_buffer)
  727. == SPI_COMPARE_SKIP_TEST) {
  728. /* OK, the stupid drive can't do a write echo buffer
  729. * test after all, fall back to the read tests */
  730. len = 0;
  731. goto retry;
  732. }
  733. }
  734. /** spi_dv_device - Do Domain Validation on the device
  735. * @sdev: scsi device to validate
  736. *
  737. * Performs the domain validation on the given device in the
  738. * current execution thread. Since DV operations may sleep,
  739. * the current thread must have user context. Also no SCSI
  740. * related locks that would deadlock I/O issued by the DV may
  741. * be held.
  742. */
  743. void
  744. spi_dv_device(struct scsi_device *sdev)
  745. {
  746. struct scsi_target *starget = sdev->sdev_target;
  747. u8 *buffer;
  748. const int len = SPI_MAX_ECHO_BUFFER_SIZE*2;
  749. if (unlikely(scsi_device_get(sdev)))
  750. return;
  751. buffer = kmalloc(len, GFP_KERNEL);
  752. if (unlikely(!buffer))
  753. goto out_put;
  754. memset(buffer, 0, len);
  755. /* We need to verify that the actual device will quiesce; the
  756. * later target quiesce is just a nice to have */
  757. if (unlikely(scsi_device_quiesce(sdev)))
  758. goto out_free;
  759. scsi_target_quiesce(starget);
  760. spi_dv_pending(starget) = 1;
  761. down(&spi_dv_sem(starget));
  762. starget_printk(KERN_INFO, starget, "Beginning Domain Validation\n");
  763. spi_dv_device_internal(sdev, buffer);
  764. starget_printk(KERN_INFO, starget, "Ending Domain Validation\n");
  765. up(&spi_dv_sem(starget));
  766. spi_dv_pending(starget) = 0;
  767. scsi_target_resume(starget);
  768. spi_initial_dv(starget) = 1;
  769. out_free:
  770. kfree(buffer);
  771. out_put:
  772. scsi_device_put(sdev);
  773. }
  774. EXPORT_SYMBOL(spi_dv_device);
  775. struct work_queue_wrapper {
  776. struct work_struct work;
  777. struct scsi_device *sdev;
  778. };
  779. static void
  780. spi_dv_device_work_wrapper(void *data)
  781. {
  782. struct work_queue_wrapper *wqw = (struct work_queue_wrapper *)data;
  783. struct scsi_device *sdev = wqw->sdev;
  784. kfree(wqw);
  785. spi_dv_device(sdev);
  786. spi_dv_pending(sdev->sdev_target) = 0;
  787. scsi_device_put(sdev);
  788. }
  789. /**
  790. * spi_schedule_dv_device - schedule domain validation to occur on the device
  791. * @sdev: The device to validate
  792. *
  793. * Identical to spi_dv_device() above, except that the DV will be
  794. * scheduled to occur in a workqueue later. All memory allocations
  795. * are atomic, so may be called from any context including those holding
  796. * SCSI locks.
  797. */
  798. void
  799. spi_schedule_dv_device(struct scsi_device *sdev)
  800. {
  801. struct work_queue_wrapper *wqw =
  802. kmalloc(sizeof(struct work_queue_wrapper), GFP_ATOMIC);
  803. if (unlikely(!wqw))
  804. return;
  805. if (unlikely(spi_dv_pending(sdev->sdev_target))) {
  806. kfree(wqw);
  807. return;
  808. }
  809. /* Set pending early (dv_device doesn't check it, only sets it) */
  810. spi_dv_pending(sdev->sdev_target) = 1;
  811. if (unlikely(scsi_device_get(sdev))) {
  812. kfree(wqw);
  813. spi_dv_pending(sdev->sdev_target) = 0;
  814. return;
  815. }
  816. INIT_WORK(&wqw->work, spi_dv_device_work_wrapper, wqw);
  817. wqw->sdev = sdev;
  818. schedule_work(&wqw->work);
  819. }
  820. EXPORT_SYMBOL(spi_schedule_dv_device);
  821. /**
  822. * spi_display_xfer_agreement - Print the current target transfer agreement
  823. * @starget: The target for which to display the agreement
  824. *
  825. * Each SPI port is required to maintain a transfer agreement for each
  826. * other port on the bus. This function prints a one-line summary of
  827. * the current agreement; more detailed information is available in sysfs.
  828. */
  829. void spi_display_xfer_agreement(struct scsi_target *starget)
  830. {
  831. struct spi_transport_attrs *tp;
  832. tp = (struct spi_transport_attrs *)&starget->starget_data;
  833. if (tp->offset > 0 && tp->period > 0) {
  834. unsigned int picosec, kb100;
  835. char *scsi = "FAST-?";
  836. char tmp[8];
  837. if (tp->period <= SPI_STATIC_PPR) {
  838. picosec = ppr_to_ps[tp->period];
  839. switch (tp->period) {
  840. case 7: scsi = "FAST-320"; break;
  841. case 8: scsi = "FAST-160"; break;
  842. case 9: scsi = "FAST-80"; break;
  843. case 10:
  844. case 11: scsi = "FAST-40"; break;
  845. case 12: scsi = "FAST-20"; break;
  846. }
  847. } else {
  848. picosec = tp->period * 4000;
  849. if (tp->period < 25)
  850. scsi = "FAST-20";
  851. else if (tp->period < 50)
  852. scsi = "FAST-10";
  853. else
  854. scsi = "FAST-5";
  855. }
  856. kb100 = (10000000 + picosec / 2) / picosec;
  857. if (tp->width)
  858. kb100 *= 2;
  859. sprint_frac(tmp, picosec, 1000);
  860. dev_info(&starget->dev,
  861. "%s %sSCSI %d.%d MB/s %s%s%s%s%s%s%s%s (%s ns, offset %d)\n",
  862. scsi, tp->width ? "WIDE " : "", kb100/10, kb100 % 10,
  863. tp->dt ? "DT" : "ST",
  864. tp->iu ? " IU" : "",
  865. tp->qas ? " QAS" : "",
  866. tp->rd_strm ? " RDSTRM" : "",
  867. tp->rti ? " RTI" : "",
  868. tp->wr_flow ? " WRFLOW" : "",
  869. tp->pcomp_en ? " PCOMP" : "",
  870. tp->hold_mcs ? " HMCS" : "",
  871. tmp, tp->offset);
  872. } else {
  873. dev_info(&starget->dev, "%sasynchronous.\n",
  874. tp->width ? "wide " : "");
  875. }
  876. }
  877. EXPORT_SYMBOL(spi_display_xfer_agreement);
  878. #define SETUP_ATTRIBUTE(field) \
  879. i->private_attrs[count] = class_device_attr_##field; \
  880. if (!i->f->set_##field) { \
  881. i->private_attrs[count].attr.mode = S_IRUGO; \
  882. i->private_attrs[count].store = NULL; \
  883. } \
  884. i->attrs[count] = &i->private_attrs[count]; \
  885. if (i->f->show_##field) \
  886. count++
  887. #define SETUP_RELATED_ATTRIBUTE(field, rel_field) \
  888. i->private_attrs[count] = class_device_attr_##field; \
  889. if (!i->f->set_##rel_field) { \
  890. i->private_attrs[count].attr.mode = S_IRUGO; \
  891. i->private_attrs[count].store = NULL; \
  892. } \
  893. i->attrs[count] = &i->private_attrs[count]; \
  894. if (i->f->show_##rel_field) \
  895. count++
  896. #define SETUP_HOST_ATTRIBUTE(field) \
  897. i->private_host_attrs[count] = class_device_attr_##field; \
  898. if (!i->f->set_##field) { \
  899. i->private_host_attrs[count].attr.mode = S_IRUGO; \
  900. i->private_host_attrs[count].store = NULL; \
  901. } \
  902. i->host_attrs[count] = &i->private_host_attrs[count]; \
  903. count++
  904. static int spi_device_match(struct attribute_container *cont,
  905. struct device *dev)
  906. {
  907. struct scsi_device *sdev;
  908. struct Scsi_Host *shost;
  909. struct spi_internal *i;
  910. if (!scsi_is_sdev_device(dev))
  911. return 0;
  912. sdev = to_scsi_device(dev);
  913. shost = sdev->host;
  914. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  915. != &spi_host_class.class)
  916. return 0;
  917. /* Note: this class has no device attributes, so it has
  918. * no per-HBA allocation and thus we don't need to distinguish
  919. * the attribute containers for the device */
  920. i = to_spi_internal(shost->transportt);
  921. if (i->f->deny_binding && i->f->deny_binding(sdev->sdev_target))
  922. return 0;
  923. return 1;
  924. }
  925. static int spi_target_match(struct attribute_container *cont,
  926. struct device *dev)
  927. {
  928. struct Scsi_Host *shost;
  929. struct scsi_target *starget;
  930. struct spi_internal *i;
  931. if (!scsi_is_target_device(dev))
  932. return 0;
  933. shost = dev_to_shost(dev->parent);
  934. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  935. != &spi_host_class.class)
  936. return 0;
  937. i = to_spi_internal(shost->transportt);
  938. starget = to_scsi_target(dev);
  939. if (i->f->deny_binding && i->f->deny_binding(starget))
  940. return 0;
  941. return &i->t.target_attrs.ac == cont;
  942. }
  943. static DECLARE_TRANSPORT_CLASS(spi_transport_class,
  944. "spi_transport",
  945. spi_setup_transport_attrs,
  946. NULL,
  947. NULL);
  948. static DECLARE_ANON_TRANSPORT_CLASS(spi_device_class,
  949. spi_device_match,
  950. spi_device_configure);
  951. struct scsi_transport_template *
  952. spi_attach_transport(struct spi_function_template *ft)
  953. {
  954. struct spi_internal *i = kmalloc(sizeof(struct spi_internal),
  955. GFP_KERNEL);
  956. int count = 0;
  957. if (unlikely(!i))
  958. return NULL;
  959. memset(i, 0, sizeof(struct spi_internal));
  960. i->t.target_attrs.ac.class = &spi_transport_class.class;
  961. i->t.target_attrs.ac.attrs = &i->attrs[0];
  962. i->t.target_attrs.ac.match = spi_target_match;
  963. transport_container_register(&i->t.target_attrs);
  964. i->t.target_size = sizeof(struct spi_transport_attrs);
  965. i->t.host_attrs.ac.class = &spi_host_class.class;
  966. i->t.host_attrs.ac.attrs = &i->host_attrs[0];
  967. i->t.host_attrs.ac.match = spi_host_match;
  968. transport_container_register(&i->t.host_attrs);
  969. i->t.host_size = sizeof(struct spi_host_attrs);
  970. i->f = ft;
  971. SETUP_ATTRIBUTE(period);
  972. SETUP_RELATED_ATTRIBUTE(min_period, period);
  973. SETUP_ATTRIBUTE(offset);
  974. SETUP_RELATED_ATTRIBUTE(max_offset, offset);
  975. SETUP_ATTRIBUTE(width);
  976. SETUP_RELATED_ATTRIBUTE(max_width, width);
  977. SETUP_ATTRIBUTE(iu);
  978. SETUP_ATTRIBUTE(dt);
  979. SETUP_ATTRIBUTE(qas);
  980. SETUP_ATTRIBUTE(wr_flow);
  981. SETUP_ATTRIBUTE(rd_strm);
  982. SETUP_ATTRIBUTE(rti);
  983. SETUP_ATTRIBUTE(pcomp_en);
  984. SETUP_ATTRIBUTE(hold_mcs);
  985. /* if you add an attribute but forget to increase SPI_NUM_ATTRS
  986. * this bug will trigger */
  987. BUG_ON(count > SPI_NUM_ATTRS);
  988. i->attrs[count++] = &class_device_attr_revalidate;
  989. i->attrs[count] = NULL;
  990. count = 0;
  991. SETUP_HOST_ATTRIBUTE(signalling);
  992. BUG_ON(count > SPI_HOST_ATTRS);
  993. i->host_attrs[count] = NULL;
  994. return &i->t;
  995. }
  996. EXPORT_SYMBOL(spi_attach_transport);
  997. void spi_release_transport(struct scsi_transport_template *t)
  998. {
  999. struct spi_internal *i = to_spi_internal(t);
  1000. transport_container_unregister(&i->t.target_attrs);
  1001. transport_container_unregister(&i->t.host_attrs);
  1002. kfree(i);
  1003. }
  1004. EXPORT_SYMBOL(spi_release_transport);
  1005. static __init int spi_transport_init(void)
  1006. {
  1007. int error = transport_class_register(&spi_transport_class);
  1008. if (error)
  1009. return error;
  1010. error = anon_transport_class_register(&spi_device_class);
  1011. return transport_class_register(&spi_host_class);
  1012. }
  1013. static void __exit spi_transport_exit(void)
  1014. {
  1015. transport_class_unregister(&spi_transport_class);
  1016. anon_transport_class_unregister(&spi_device_class);
  1017. transport_class_unregister(&spi_host_class);
  1018. }
  1019. MODULE_AUTHOR("Martin Hicks");
  1020. MODULE_DESCRIPTION("SPI Transport Attributes");
  1021. MODULE_LICENSE("GPL");
  1022. module_init(spi_transport_init);
  1023. module_exit(spi_transport_exit);